Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

unity3d - Check Game Object Name Unity

I Attempting to make a Script in Unity 2D.

Basically, when the Player touches a door collider, it is for him to do that "Degub.Log"

    using UnityEngine;
    
    public class ChangeScene : MonoBehaviour
    {
    
        void OnCollisionEnter2D(Collision2D collision)
        {
            if(collision.gameObject.name == "Door")
            {
                Debug.Log("Is On Door");
            }
        }
    }

But, when i Toutch the Collisor, nothing happens.

How i Can Check if the collision game object name is "door"?

*I don't want to compare with any tag or layer, I want to compare with the name itself.

question from:https://stackoverflow.com/questions/65879633/check-game-object-name-unity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
public class ChangeScene : MonoBehaviour
    {
    
        void OnCollisionEnter2D(Collision2D collision)
        {
            if(collision.gameObject.name == "door")
                {
                    Debug.Log("Is on Door");
                }
        }
    }

And this script should be on the GameObject (Door) to check for other gameobjects it collides with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...