- Home /
I am not able to call a function from OntriggerEnter event :(
Hi guys am a total noobie in unity, actually am trying to add scoe when bullet hits the enemies collider. But i cant call the function from OntriggerEnter event. dont know what went wrong
void OnTriggerEnter(Collider col)
{
//first disable the zombie's collider so multiple collisions cannot occur
GetComponent<CapsuleCollider>().enabled = false;
//destroy the bullet
Destroy(col.gameObject);
//stop the zombie from moving forward by setting its destination to it's current position
agent.destination = gameObject.transform.position;
//stop the walking animation and play the falling back animation
GetComponent<Animation>().Stop();
GetComponent<Animation>().Play("fallingback");
Controllerscript.AddScore(scoreValue);//this part is not working for me.
//destroy this zombie in six seconds.
Destroy(gameObject, 6);
//instantiate a new zombie
GameObject zombie = Instantiate(Resources.Load("zombie", typeof(GameObject))) as GameObject;
//set the coordinates for a new vector 3
float randomX = UnityEngine.Random.Range(-12f, 12f);
float constantY = .01f;
float randomZ = UnityEngine.Random.Range(-13f, 13f);
//set the zombies position equal to these new coordinates
zombie.transform.position = new Vector3(randomX, constantY, randomZ);
//if the zombie gets positioned less than or equal to 3 scene units away from the camera we won't be able to shoot it
//so keep repositioning the zombie until it is greater than 3 scene units away.
while (Vector3.Distance(zombie.transform.position, Camera.main.transform.position) <= 3)
{
randomX = UnityEngine.Random.Range(-12f, 12f);
randomZ = UnityEngine.Random.Range(-13f, 13f);
zombie.transform.position = new Vector3(randomX, constantY, randomZ);
}
},void OnTriggerEnter(Collider col)
{
//first disable the zombie's collider so multiple collisions cannot occur
GetComponent<CapsuleCollider>().enabled = false;
//destroy the bullet
Destroy(col.gameObject);
//stop the zombie from moving forward by setting its destination to it's current position
agent.destination = gameObject.transform.position;
//stop the walking animation and play the falling back animation
GetComponent<Animation>().Stop();
GetComponent<Animation>().Play("fallingback");
Controllerscript.AddScore(scoreValue);// this part is not working for me
//destroy this zombie in six seconds.
Destroy(gameObject, 6);
//instantiate a new zombie
GameObject zombie = Instantiate(Resources.Load("zombie", typeof(GameObject))) as GameObject;
//set the coordinates for a new vector 3
float randomX = UnityEngine.Random.Range(-12f, 12f);
float constantY = .01f;
float randomZ = UnityEngine.Random.Range(-13f, 13f);
//set the zombies position equal to these new coordinates
zombie.transform.position = new Vector3(randomX, constantY, randomZ);
//if the zombie gets positioned less than or equal to 3 scene units away from the camera we won't be able to shoot it
//so keep repositioning the zombie until it is greater than 3 scene units away.
while (Vector3.Distance(zombie.transform.position, Camera.main.transform.position) <= 3)
{
randomX = UnityEngine.Random.Range(-12f, 12f);
randomZ = UnityEngine.Random.Range(-13f, 13f);
zombie.transform.position = new Vector3(randomX, constantY, randomZ);
}
}
Use the "101010" button to post your code - it's not properly readable as it is.
Having said that, to me it looks like this wouldn't even compile, because of this line (currently line 17):
},void OnTriggerEnter(Collider col)
I would expect that comma to generate an "Invalid Token" error
So
Reformat the code part of your post and
Post the actual code
Answer by Jack-Mariani · May 15, 2019 at 10:23 AM
If the OnTriggerEnter does not work it could mean you've not set it uo properly.
One of the colliding objects would require a RigidBody.
You may also check this tutorial: https://www.youtube.com/watch?v=m0fjrQkaES4
Answer by tormentoarmagedoom · May 15, 2019 at 10:48 AM
Hello.
First, As jack says, first be sure the method is beeing called.
Second, why post all this huge code just for this question? If pretend someone to answer you delete all unnecessary information, and give the information needed you dont post, like screenshot of inspector of the object....
Bye.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201430im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to call OnTriggerEnter once 5 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer
How to trigger an event if the collided object has the tag “Player” 3 Answers