using an array

Hello World,Hope every one is fine
i ve got an array of objects,want to instantiate those objects one by one,and move them towards a specific point,but when a new object is instantiated i lose control on the previous object and that stops moving.plz help

Well, whenever you create a new object you are overriding the transformer variable with your new instance of the object. There are a few ways around this, I’ll just name the 2 most obvious ones.

  1. Use a list of atransforms to move all of your instantiated objects inside of the update.
private List<Transform> atransforms;

private void Update()
{
    foreach(Transform atransform in atransforms)
    {
        // Make sure to delete the global atransform variable first.
        atransform.Translate(-speed, 0, 0);
    }
}

void Thrower()
{
    Transform atransform = Intantiate(gameObjects[i], new Vector(-5, 0, 0), Quaternion.identity);
    atransforms.Add(atransform);
}
  1. Use Coroutines. This is what I would do. Just convert Update into an endless Coroutine with a yield return null. Then call that coroutine under line 22 while passing in that transform as a parameter.
1 Like

Ohh man :slight_smile: Graciousness at its best,thanks alot.

// Make sure to delete the global atransform variable first.
Dude what did you mean by this in line 7?

In your original image that you sent, delete line 10. That way there are no conflicts with names.