Move objects in Array??

Hi everyone!

I’m trying to move objects of an array. First I have created an instance of objects and work fine, but when i want to move that objects, don’t work for anything.

This is my code:

public float x, y, z;
    public GameObject[] Vehiculos;
    public float spawnTime;
    public Transform[] spawnPoints;

    void Start () {
        InvokeRepeating ("SpawnController", spawnTime, spawnTime);
    }

    void Update(){
        //THIS IS THE MOVEMENT AND DOESN'T WORK
        foreach(GameObject vehiculos in Vehiculos ){
            vehiculos.transform.position += new Vector3(x,y,z * Time.deltaTime);
        }
    }

    void SpawnController () {
        int spawnPointIndex = Random.Range (0, spawnPoints.Length);
        Instantiate (Vehiculos [Random.Range (0,Vehiculos.Length)], spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);

    }
}

it would make a lot more sense to add the movement script to the actual vehicles themselves rather than trying to move them remotely.

In the code above you are not moving the instantiated object, you are “moving” the prefabs, which is fairly irrelevant since the instantiate set’s their position when they are created into the scene.

if you want to move them remotely you will need a reference to them as they are created so that you can store the instances, i.e.

don’t just copy this code, it’s just as an example

Using System.Collections.Generic;

List<GameObject> myInstances = new List<GameObject>();


GameObject clone = Instantiate (....
myInstance.Add(clone);

foreach(GameObject v in myInstances) // foreach has it's own issues, but you're using it so so shall I
{
//movement stuff
}

don’t use an array for this instance reference tracking, there will be variable numbers of instances in the scene so a list is more appropriate.