- Home /
Question by
choxmi · Dec 05, 2015 at 06:03 PM ·
2dinstantiatecollidertriggers
OnTriggerEnter and OnTriggerExit both trigger twice
I'm developing a 2d shooter game with some 3d aspects. I have used a box collider for the player and another box collider for the trigger object. Every time the player trigger that object collider it should instantiate only one object. But it create two objects. Tried adding boolean variable and all. Could not find an answer. Please help.
#pragma strict
public var count: int;
function OnTriggerExit (other: Collider) {
if(other.gameObject.tag == "Player"){
Debug.Log(other.gameObject.name);
count = Random.Range(1,5);
Debug.Log(count);
if(count == 1)
{
Instantiate(GameObject.FindGameObjectWithTag("obj1"), new Vector3(transform.position.x + (44), 0, 0), Quaternion.identity);
}
else if(count == 2)
{
Instantiate(GameObject.FindGameObjectWithTag("obj2"), new Vector3(transform.position.x + (44), 0, 0), Quaternion.identity);
}
else if(count == 3)
{
Instantiate(GameObject.FindGameObjectWithTag("obj3"), new Vector3(transform.position.x + (44), 0, 0), Quaternion.identity);
}
else if(count == 4)
{
Instantiate(GameObject.FindGameObjectWithTag("obj4"), new Vector3(transform.position.x + (44), 0, 0), Quaternion.identity);
}
else
{
Instantiate(GameObject.FindGameObjectWithTag("obj1"), new Vector3(transform.position.x + (44), 0, 0), Quaternion.identity);
}
}
}
Thank you
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612051437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player respawn with 2D collision not working 1 Answer
Detecting what parts of the terrain was hit. 2 Answers
PolygonColliders act different on a build 2 Answers
Only 1 of 3 conditions being executed in IF statement?(Solved) 1 Answer
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer