Transitioning animations based on conditions from a different script.

Every time on Key Up, a coroutine is run, it makes the boolean ‘blasting’ true at the start of the coroutine, does some other things, and just before the coroutine ends, it makes the boolean false. What I want it to happen is that when ‘blasting’ becomes true, it should change my animation from Idle to the Jump animation. I created a bool parameter in the ‘Animator’ called Jumping and created transitions from Idle to Jump based on when Jumping becomes true, and from Jump to Idle based on Exit Time.
I’m checking for the ‘blasting’ boolean to be true and setting the Jumping boolean to true but the Jump animation never plays. I also have the Animator controller on my Player object and Apply Root Motion is ticked off on it. Here is the code.

private Animator anim;

void Start (){
		anim = GetComponent<Animator> ();
}

void Update () {
if (blast.GetComponent<InstantiateBlast> ().blasting) 
		{
			anim.SetBool("Jumped",true);
		}
}

Update: Upon further checking, on adding debug statements inside the if statement, nothing is getting debugged. I’m getting errors in Unity that say - NullReferenceException: Object reference not set to an instance of an object - for the line - if (blast.GetComponent ().blasting). Now, my blast object keeps getting activated and deactivated due to other collisions in the scene. How do I make sure that the NullReferenceException stops occuring and allowing for the checking of ‘blasting’ to continue ?

Handling Errors

So you want unity to ignore the Null-Reference error? well all you do is check if its null and return if its null. To do this we need to Catch the Error when it happens withing that level of code. this will bypass the halting effect that errors usually have(catching will not work with compiler errors!)

The Try and Catch Keyword

The try keyword is used to run code that may or may not work correctly, the code inside is refereed to as “guarded code”. code inside try will run like normal until an exception is found this is where out catch comes in. Every time you get a run time error, your computer will look for the catch statement that handles that specific error.

if no catch statement is found then the program is halted until the error is fixed or the programmers adds the handling.when there is a catch statement, the compiler will run the code withing the catch statement without halting the program. Unity handles exception in a way so that when you get an exception, An error message appears in the Unity console and halts that code, not all of unity itself.

there is also a finally keyword along with catch and try but its not needed here.

Usage

we put the code we want into the try statement. then below it we pass an error type to the catch statement. in this case its is the dreaded NullReferenceException. Now when anything in that code gets a Null referencing Exception, instead of halting execution the catch statement will run, from here I like to add a debug statement so I know that the error is there and is being caught. then we can just call a “return” statment.

something like this

try{
if( blast.GetComponent<InstantiateBlast> ().blasting) {
anim.SetBool("Jumped",true);
}
}
catch(System.NullReferenceException ne) {
Debug.Log("Null reference Exception: Your Arguement is Invalid "); //tells you the error, but does not hinder the code from running
return;
}

Yes there are called try and catch statments see here:

Give it a read, try-catch and finally statements fit this purpose as you don’t always know if something will be there.

Edit: Yes, most scripts i post are usually a guide not a paste off solution.

also note don’t just whack a catch statement every time you get an error, this will cause more bad than good