0
\$\begingroup\$

I have a script, I am using this script to instantiate new path and also wall (as border) to generate new path and wall. Before some time all scripts work fine. but at this time my player enter in empty trigger collider 3 times to generate new path.

I am using capsule as player and also capsule collider. when my game start my player enter 2/3 times to empty collider(PathSpawnCollider) so it give me unwanted path and boundary walls.

Any one can help me what is problem here in my code.?

PathSpawnCollider.cs

using UnityEngine;
using System.Collections;

public class PathSpawnCollider : MonoBehaviour {

public float positionY = 0.81f;
public Transform[] PathSpawnPoints;
public GameObject Path;
public GameObject DangerousBorder;
public Transform[] BorderSpawnPoints;


void OnTriggerEnter(Collider hit)
{
    //player has hit the collider
    if (hit.gameObject.tag == "Player") {   

            //find whether the next path will be straight, left or right
            int randomSpawnPoint = Random.Range (0, PathSpawnPoints.Length);
            //int randomBSP = Random.Range (0, BorderSpawnPoints.Length);
            for (int i = 0; i < PathSpawnPoints.Length; i++) {

                //instantiate the path, on the set rotation
            if (i == randomSpawnPoint) {
                Debug.LogError ("Value of if i : " + i);
                Instantiate (Path, PathSpawnPoints [i].position, PathSpawnPoints [i].rotation);
            }
                else {
                Debug.LogError ("Value of else i : "+i);
                    //instantiate the border, but rotate it 90 degrees first
                Vector3 rotation = BorderSpawnPoints [i].rotation.eulerAngles;
                    rotation.y += 90;
                Vector3 position = BorderSpawnPoints [i].position;
                    position.y += positionY;
                    Instantiate (DangerousBorder, position, Quaternion.Euler (rotation)); 
                }
            } 
        }
    }

}

\$\endgroup\$
1
\$\begingroup\$

I solve my question with some different logic.

I have changed for loop to while loop and increase I in last of all code, and problem solved.

while(i < PathSpawnPoints.Length){

//all code here

i++;
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.