Unity new spawned object follows wrong path

Viewed 28

I have two paths for following. At one time there should spawn 1 object and there should be at left and right in turn (Let's say, the object spawned at right then the next one will be at left).Image for path logic

Follow Path Script

For this, I added an if clause like this:

if (PlayerPrefs.GetString("Spawn") == "Left")
    {
        randomInt = Random.Range(0, spawnPointsLeft.Length);
        Instantiate(prefab, spawnPointsLeft[randomInt].position, Quaternion.identity);
    }

and tried this kind of filter :

 if (left)
 {
   randomInt = Random.Range(0, spawnPointsLeft.Length);
   Instantiate(prefab, spawnPointsLeft[randomInt].position, Quaternion.identity);
   left = false;
   right = true;
 }
 else if (right)
 {
   randomInt = Random.Range(0, spawnPointsRight.Length);
   Instantiate(prefab, spawnPointsRight[randomInt].position, Quaternion.identity);
   left = true;
   right = false;
 }

Here is the problem, when I add the if clauses new spawned objects follow just left path.( Even if it spawned at right spawn point) . But when I filter for left it works well. I don't understand what is problem or what did I do wrong?

I'll be glad if you can help me :)

1 Answers
  1. Inside of the clause, you could add an int variable for what path it is going on. The reason I use an int and not a bool, is for if you want more paths later on.
  2. Set a Vector3 or Transform array for the paths you want it to follow.

This way you can easily find the corresponding path.

Let me know by commenting if this doesn't work and make sure to provide info why.

Related