How can I track positions of instantiated objects

Hi guys,

I’m new(ish) to unity and… need some help here.

My brain says something like this “should” work but it looks like unity doesn’t really like it :

	void Update () {
                //Oject that flies across screen from right to left
		GameObject obs;

		float leftScreen = Camera.main.orthographicSize * -1f;
                obs = Spawn ();
		//Shouldn't the below keep track of the position of obs?
                Debug.Log (obs.transform.position.x);

	}

        //Spawning method
	private GameObject Spawn () {
		int r = Random.Range (0, goList.Count);
		Particle = true;
		return Instantiate (partList [r], new Vector3 (Camera.main.orthographicSize+10, 0, 0), Quaternion.identity) as GameObject;
		Debug.Log (partList [r].transform.position.x);
	}

So what I need is an object that flies across the screen (camera view) from right to the left and when the object goes beyond the left side of the screen i want it to do a Destory (obs) on it.
But when I used obs.tranform.position.x it seems like unity isn’t keep a track of obs dynamically and only gets the starting point…

Is there any tips on what I may be doing wrong here and could you guys help me out?

Thanks in advance.

If you doing that. You will lose information of object that spawn before.

I recommand you this.

List<GameObject> objs = new List<GameObjects>();
void Update ()
{
     //Oject that flies across screen from right to left
     GameObject obs;

     float leftScreen = Camera.main.orthographicSize * -1f;
     obs = Spawn ();
     objs.Add(obs);
}

//If you want to track all object you spawn just do something like this
void OnGUI()
{
    foreach (GameObject go in objs)
    {
           GUILayout.Label(go.transform.position.x);
           //Or debug
           Debug.Log(go.transform.position.x);
    }
}

But It might slow down if you just spawn every update.

Maybe you can make it spawn everytime you press a key?

Like this

void Update ()
{
     if (Input.GetKeyDown(Keycode.F)
    {
         //Oject that flies across screen from right to left
         GameObject obs;

         float leftScreen = Camera.main.orthographicSize * -1f;
         obs = Spawn ();
         objs.Add(obs);
    }
}

And object will spawn everytime you press F key.

Thanks for the help :slight_smile:
Made a better starting point for me.