Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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;