public string tag ;

Description

此游戏对象的标签。

可使用标签来识别游戏对象。 使用标签前,必须在标签和层管理器中声明它们。

注意:您不应通过 Awake()OnValidate() 方法设置标签。这是因为组件唤醒的顺序是不确定的,因此可能导致意外行为,例如标签在唤醒时被覆盖。如果尝试执行此操作,Unity 将生成一条警告:“SendMessage cannot be called during Awake, CheckConsistency, or OnValidate”。

using UnityEngine;

public class CollisionGameObjectExample : MonoBehaviour { //Detect collisions between the GameObjects with Colliders attached void OnCollisionEnter(Collision collision) { //Check for a match with the specified name on any GameObject that collides with your GameObject if (collision.gameObject.name == "MyGameObjectName") { //If the GameObject's name matches the one you suggest, output this message in the console Debug.Log("Do something here"); }

//Check for a match with the specific tag on any GameObject that collides with your GameObject if (collision.gameObject.tag == "MyGameObjectTag") { //If the GameObject has the same tag as specified, output this message in the console Debug.Log("Do something else here"); } } }