繁体   English   中英

c# - 如何在统一中编写不等于

[英]How to Write a not equal to in unity c#

如何在unity c#中的if语句上写一个不等于。 我想写

 if (other.CompareTag not equal to ("Score_Trigger")){ }

我试过

if(!other.CompareTag("Score_Trigger"))
       {}

查看统一文档https://docs.unity3d.com/ScriptReference/Component.CompareTag.html

用法描述为

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        Destroy(other.gameObject);
    }
}

鉴于你只做了 other.CompareTag,也许这就是你的代码失败的原因。 其他将是对撞机/触发器,当然没有标签

因此测试应该是if (!other.gameObject.CompareTag("Score_Trigger")...

我测试了你的尝试,它通过没有任何问题。 检查是否正确设置了此 if 语句之外的所有内容。 请注意,您尝试比较的对象需要一个标签。

if (!a.CompareTag("test"))
{
    Debug.Log("is not equal...");
}

好吧,如果您尝试的方法不起作用,那么您可以尝试以下解决方案之一:

 if (other.gameObject.CompareTag("Score_Trigger"))
        {
            Debug.Log("Equal");
        }
        else
        {
         Debug.Log("Not Equal");
        }

if(other.gameObject.CompareTag("Score_Trigger") == false)
{
 ...
}

您的第二次尝试应该可以正常工作,但您也可以这样做

if (other.tag != "Score_Trigger")

或者

if (other.gameObject.tag != "Score_Trigger")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM