Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolbird22 · May 25, 2014 at 06:03 AM · animationjumptransition

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 ?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · May 25, 2014 at 08:37 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · May 27, 2014 at 03:14 PM 0
Share

Thanks a lot for explaining this. Seems like quite an important thing to know indeed ! Thanks yet again !

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges