- Home /
How to trigger an event if the collided object has the tag “Player”
I have a trigger zone around a spider terrarium in my game and would like that to trigger the spider's animation when the player walks into the trigger, here is my attempt, but it is returning an error, "The name 'col' does not exist in the current context".
public class Spider: MonoBehaviour {
Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision collision)
{
if (col.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
}
Shouldn't it be: if (collision.gameObject.tag == "Player")
Answer by TanselAltinel · May 05, 2018 at 07:10 PM
I've updated the answer. Please use this code. Your error was using coll in the function input. They have to be same.
Hi,
Without also sharing the error, it is not possible for us to guess what is wrong.
However, the correct usage of tag comparison should be as follows:
public class Spider: MonoBehaviour {
private Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision coll)
{
if (col.gameObject.CompareTag("Player"))
{
anim.SetTrigger("spider");
}
}
}
Make sure game object has Animator component; also player object to have tag Player set.
Answer by Nixmortem · May 06, 2018 at 07:43 AM
If you are using a collider set to be a trigger, you want to use the OnTriggerEnter Method. They're pretty similar but usually if you have "Player enters area = make this thing happen" the collider component on the object (the area you're using as the spider's trigger) is a trigger. So your Trigger Method could look like this:
private void OnTriggerEnter(Collider collider)
{
if(collider.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
When you use the OnTriggerEnter Method, in the parentheses you're passing through a parameter, kind of like a variable. When you go to use it, like in the if statement to check the tag, you have to make sure it is spelled exactly like it is when you declare it. So in your code you have Collision collision but in your if statement you have it as just col. That is why you're getting the "The name 'col' does not exist in the current context" error. You never declared a variable with the name col.
Answer by KittenSnipes · May 06, 2018 at 10:20 AM
@AbdulhamidB7 You copy and pasted but that is fine. You need to look a little closer at the code next time. You called for a variable in the function named collision. That is your input variables name but instead of using it you started using a variable that was never declared called Col. That was your problem. Now the script should work as intended.
public class Spider: MonoBehaviour {
Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
Your answer
Follow this Question
Related Questions
How can I animate a GameObject that isn't parented, but will need to be for the animation? 1 Answer
chronological order of collision to give a winner unity 2d 4.3 1 Answer
How do I create two colliding Spheres? 1 Answer
Attack animation Wont Play 1 Answer
How to disable an certain animation from my player? 0 Answers