1

I have a project in my collage for traffic car simulation in unity, and the one of the project requirements is to let the user enter the number of the cars, and each one of them should driving like AI_Driver, and i wrote a script to spawning cars with Instantiate function , i spawned 1000 car and when i am spawning a car, i am making some changes like (the position of the spawned car on the y axis),

the problem is some of the cars has been edited correctly exactly what i want but, the others not applying the changes with the position .

and here is the script:

public class VehicleSpawner : MonoBehaviour
{
    public GameObject[] vehiclePrefabs;
    public int vehiclesToSpawn;
    public bool navON = false;
    public bool aStarON = true;

    void Start()
    {
        StartCoroutine(SpawnMany(0));
    }

    IEnumerator SpawnMany(int count)
    {
        while (count < vehiclesToSpawn)
        {
            GameObject obj = Instantiate(vehiclePrefabs[1]);
            GameObject[] spawningPints = GameObject.FindGameObjectsWithTag("spawningPoint");
            Transform child = spawningPints[Random.Range(0, spawningPints.Length)].transform;

            WaypointNavigator nav = obj.GetComponent<WaypointNavigator>();
            nav.spawner = this;
            nav.priority = Random.Range(0, 2 * vehiclesToSpawn);

            if (!aStarON)
            {
                nav.currentWaypoint = child.GetComponent<Waypoint>();
                nav.navON = navON;
            }
            else
            {
                Transform dest = transform.GetChild(Random.Range(0, transform.childCount - 1));

                nav.start = child.GetComponent<Waypoint>();
                nav.end = dest.GetComponent<Waypoint>();
                nav.priority = Random.Range(0, 10000);
                nav.aStarON = aStarON;
            }

            // change in the position
            obj.transform.position = new Vector3(
                child.GetComponent<Waypoint>().GetPosition().x,
                child.GetComponent<Waypoint>().GetPosition().y + 0.5f,
                child.GetComponent<Waypoint>().GetPosition().z
            );

            yield return new WaitForSeconds(0.05f);

            count++;
        }
    }
}

Please any one can help??

Razshal
  • 27
  • 1
  • 7
Mohamad_Hn
  • 55
  • 1
  • 6
  • I'm not quite sure what you're trying to do, or how you're trying to do it, but if you want to randomize a car's position on the y axis, use a random value, and if you want to "stack" them, or have the distance fixed, you should put a variable to record the current car's y value inside the coroutine but outside the `while` loop and increment it by however much you want for each car spawned. Use that variable for the y value of each car, too, obviously. – verified_tinker Jul 26 '20 at 15:10

1 Answers1

0
  1. Cache spawningPints. Never use Find, because it just runs foreach gameObject in the hierarchy.
  2. Try first to change position, then add Nav component (it might cache position and teleport car back after you change position throw transform).
Whitebrim
  • 396
  • 2
  • 9