Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Make an object randomly loop in C#

Hi Everyone, I've asked this question several times on Unity Threads, but I'm not really getting anywhere. So I have an enemy in my 2D game that loops around every time he reaches the end of the scene, but it's really boring to have him just loop around in the same place on the y axis. Could someone help me with the script for making him loop around in a random height position? Thanks! Here is my code so far, and it's a side scrolling game:

public float scrollSpeed; public float tileSizeX; private Vector3 startPosition;

// Use this for initialization
void Start () {

    startPosition = transform.position;
    transform.position = new Vector3(transform.position.x, Random.Range(0, 100), transform.position.z);
}

// Update is called once per frame
void Update () {

    float newPosition = Mathf.Repeat(Time.time*scrollSpeed,tileSizeX);
    transform.position = startPosition + Vector3.left * newPosition;

}