Hey guys, I’m a big newbie with C# and I’m currently struggling with a big problem. I have a Game Object which is moving over named colliders without mesh renderers (invisible). Now basically want to check on which collider the object is currently on. Is there any possibility to do that with an if statement?
I appreciate any help.
Try tagging the collider with a specific tag in Unity and checking for that tag in your player script maybe? I am a newb too, so maybe we can figure this out together. lol
When you define a collision callback on the object, you get a reference to the other collider involved in the collision. You can save a reference to that collider OnEnter and remove the reference OnExit.
To identify a collider, for instance, if one collider is a “FinishLine” or something, then you should give that collider object a new component called “FinishLine”. Then you can run code that looks something like this:
// check if the current collider has the FinishLine component
public bool AtFinishLine()
{
return this.currentCollider != null && this.currentCollider.GetComponent<FinishLine>() != null;
}
It’s much easier and more robust to identify an object by what components it has, rather than it’s name or tag. Objects can have multiple components, but only one name or tag, which are also subject to typos and changes.