So i have this speech recognition code with a few commands that trigger animations in C# and 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. The solution I came up with is string interpellation.
The problem is I keep getting the error:
Object reference not set to an instance of an object.
In Unity pertaining to the line where (“{outro}”) is referenced and I’m not sure but maybe it’s because I’m trying to use string interpolation across 2 if statements?
P.S without the additional if statement this script works it still runs in Unity with the 2nd if statement but it doesn’t do what it’s supposed to.
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);
}
So upon receiving many responses to this post I am able to narrow my problem down to one simple question.
Is it possible to reference a variable from an if statement?
Because I do need the if statement unless their is some other method that does the exact same thing. The only issue now is the variable “outro” is obviously not even a thing outside of the if statement. I believe if I could reference outro properly this code would work 100%