- Home /
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 ?
Unity gives an error for the 6th line of that code - The type or namespace name `NullReferenceException' could not be found. Are you missing a using directive or an assembly reference?
Also, I've never heard about 'try'. What does it do ?
Edit: I tested those lines inside the Update(). Is that the wrong place to catch NullReferenceException ?
Edit: Ok, the line had to be catch(System.NullReferenceException ne) for it to work. Now that, that is out of the way, it still is not executing the Jump animation. Infact, it is not even checking if the boolean 'blasting' is true as it is not returning any debug messages inside that if statement.
Edit: It all seems to work now. I was checking for the boolean inside the wrong script. Silly me. Thanks for the catching null references tip. $$anonymous$$indly post your comment as an answer so I can tag it solved.
Answer by Fornoreason1000 · May 25, 2014 at 08:14 AM
Handling Errors
o 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
he 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:
http://msdn.microsoft.com/en-us/library/0yd65esw.aspx
http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx
http://msdn.microsoft.com/en-us/library/dszsf989.aspx
http://msdn.microsoft.com/en-us/library/1ah5wsex.aspx
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
Thanks a lot for explaining this. Seems like quite an important thing to know indeed ! Thanks yet again !
Your answer
Follow this Question
Related Questions
Switching between animations 1 Answer
Transition from jump to walk animation. 1 Answer
Create animation transitions via script. 0 Answers
Talking animation with phonemes smooth transition 0 Answers
Jump to a specific frame in an animation 6 Answers