繁体   English   中英

if语句之间的字符串插值[重复]

[英]String interpolation between if statements [duplicate]

这个问题在这里已经有了答案:

所以我有这个语音识别代码,其中包含一些在 C# 和 Unity 中触发动画的命令。 So instead of having to activate each animation then deactivate it before switching to another animation I wanted to make it so that whatever animator is active it would automatically detect that, play the corresponding outro animation and go to the next animation all in one command. 我想出的解决方案是串问。

问题是我不断收到错误:

Object 引用未设置为 object 的实例。

在与引用 (“{outro}”) 的行有关的 Unity 中,我不确定,但也许是因为我试图在 2 个 if 语句中使用字符串插值?

PS 没有额外的 if 语句,这个脚本可以工作,它仍然在 Unity 中使用第二个 if 语句运行,但它没有做它应该做的事情。

if (word == “animator 2”)
{
    if (GameObject.Find(“animator 1”).activeSelf);
    {
        string outro = “animation”;
    }
    GameObject.Find(“animator 1”).GetComponent<Animator>
    ().SetBool(“{outro}”, true);
    GameObject.Find(“animator 2”).GetComponent<Animator>
    ().SetBool(“intro”, true);
}

因此,在收到对这篇文章的许多回复后,我能够将我的问题缩小到一个简单的问题。

是否可以从 if 语句中引用变量?

因为我确实需要 if 语句,除非它们是做同样事情的其他方法。 现在唯一的问题是变量“outro”显然甚至不是 if 语句之外的东西。 我相信如果我能正确引用 outro,这段代码将 100% 有效

也许这只是这里的格式,但请检查您的"符号.. 它们很奇怪。

  • 未找到您的outro字符串的原因是您在if块中声明它......在此 scope 之外不知道它。 if块之外声明变量。 您甚至可以/应该将其const ,因为它在声明和使用之间不会发生任何变化。

  • 如果您使用字符串插值,它必须以$符号开头

    SetBool($"{outro}", true);

    但是在这种情况下,您只传递了一个简单的字符串值,因此完全不需要使用字符串插值。 而是简单地传入变量本身

    SetBool(outro, true);
  • NullReferenceExceptions来自您的FindGetComponent调用。

    由于您正在检查animator 1 object 是否为activeSelf ,因此很明显在某些情况下它可能不是。

    在这种情况下Find将返回null 这可能是异常的主要来源。

    此 function 仅返回活动游戏对象。 如果找不到具有名称的 GameObject,则返回 null。 如果 name 包含 '/' 字符,它会像路径名一样遍历层次结构。

    接下来也有可能找到的 object 根本没有附加Animator器,因此GetComponent返回null

    如果游戏 object 有一个,则返回 Type 类型的组件,如果没有,则返回 null。

找出答案的最佳方法是调试并一步一步完成您的工作,并检查每个检索到的null参考。

private const string OUTRO = "animation";

// Personally in general as soon as you use strings that never change anyway I would recommend a const
private const string ANIMATOR_1 = "animator 1";
private const string ANIMATOR_2 = "animator 2";
private const string INTRO = "intro";
private const string ERROR_COMPONENT_NOT_FOUND = "The object {0} has no Animator Component!";
private const string ERROR_OBJECT_NOT_FOUND = "No object with name {0} was found! It Either doesn't exist or is not active in the hierarchy!";

...

    if (word == ANIMATOR_2)
    {
        // This is expensive! Do it only once or avoid it completely if possible!
        var animatorObj = GameObject.Find(ANIMATOR_1);

        // Not found?
        if(!animatorObj)
        {
            Debug.LogErrorFormat(this, ERROR_OBJECT_NOT_FOUND, ANIMATOR_1);
            return;
        }

        // You can completely skip the check for activeSelf!
        // If the object is not active it will never be found!

        // Also do this only once
        var animator = animatorObj.GetComponent<Animator>();

        // Component not found?
        if(!animator)
        {
            Debug.LogErrorFormat(this, ERROR_COMPONENT_NOT_FOUND, animatorObj.name);
            return;
        }

       // There is absolutely no sense of using string interpolation $""
       // If you pass in exactly one string value .. why not rather simply pass the variable itself directly? 
       animator.SetBool(OUTRO, true);
       animator.SetBool(INTRO, true);
    }

暂无
暂无

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

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