Access the position of a Transform in a Unity list

Asked

Viewed 44 times

0

I have a list of Transform that contains the Spawnpoints I have within the game, I would like to find for each of the Spawnpoints their position.

[SerializeField] List<Transform> spawns;

The problem is to give Spawn to the cars if they are within camera limits, but Spawnpoints need to be outside to give the illusion that they are born outside and not inside. How can I access the position of each Spawnpoint in the list?

IEnumerator SpawnCars()
    {
        while (OnCamera==true)//enquanto os spawns estiverem dentro do ecra
        {
            yield return new WaitForSeconds(0.5f);//esperar um segundo para spawnar
            Instantiate(car, spawns[Random.Range(0, spawns.Count)].position, Quaternion.identity, transform);//spawnar o carro na lista de spawns
        }
    }

1 answer

0

Look, I don’t know if I understand the question very well, but if I did, I would tell you to create a foreach to scroll through the list of Transform passing her position as instantiate parameter.

    IEnumerator SpawnCars()
        {
            while (OnCamera==true)//enquanto os spawns estiverem dentro do ecra
            {
                yield return new WaitForSeconds(0.5f);//esperar um segundo para spawnar
                foreach(Transform spawn in spawns){
                     Instantiate(car, spawn.position,Quaternion.identity,transform);//spawnar o carro na lista de spawns
                }
            }
       }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.