Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Dklk · Mar 28, 2016 at 02:58 PM · c#animationscripting probleminstance

Why can't I call a function within the same class? (Or: how does unity handle instancing?)

Hi, I'm new to unity and trying to learn but ran into something I don't understand. I'm trying to make a 2d sprite with animations controlled by a state variable. I attached a script to the sprite object and set my outgoing animator transitions to be triggered on the state variable, modified by script on button press. This works, but I wanted one animation to play once and stop, so I added an animation event in the animation to call a function that set the variable back to idle:

 const int stateIdle = 0;
 const int stateClick = 1;
 int _currentAnimationState;

 void Start () {
     animator = GameObject.Find("computer_dude").GetComponent<Animator>();
 }

 public void butClose()
 {        
     changeState(stateClick);        
 }

 public void resetAction()
 {
     changeState(stateIdle);
 }

 public void changeState(int state)
 {
   
    // if (_currentAnimationState == state)
    //      return;

     switch (state)
     {

         case stateIdle:
             animator.SetInteger("state", stateIdle);
             break;

         case stateClick:
             animator.SetInteger("state", stateClick);
             break;         
     }

     _currentAnimationState = state;
 }

But it doesn't stop - the value of _currentAnimationState seems to sometimes not persist and it interferes with the check on the commented out lines

If I put a break in I can see that: the first time it's called on button press - with value 1, current is 0 - this is correct The end animation calls it with value zero, but the current is still also zero (it just got set to 1?) Press the button again and now its comparing the new value of 1 to the old value of.. 1?

I can only think that its holding two instances of the class - one in the button, one in the sprite? But if so, how to I tell it to use the same instance? It wouldn't let me make it static.

Thanks!

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 Dklk · Mar 28, 2016 at 08:56 AM 0
Share

I managed to solve it with a workaround - it was different instances of the script, but I couldn't attach the animation event to the same instance as the main game one, it had to be a script in the sprite, so I just made a seperate script for it and made that script call the function in the main one.

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Interchangeable animations in a script? 0 Answers

Movement and Jump with animator 0 Answers

When I run this code Unity crashes(ANIMATOR RELATED) 0 Answers

Errors in script when trying to play animation clips 1 Answer

animator and script issue 0 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