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 Braian Correa · Dec 10, 2013 at 06:06 PM · animationanimatormecaniminstanceinstances

Animation plays for all instances of object

Guys, I really don't know what to do with this one.

I have an object, with 3 animation clips set up in the animator. They are triggered by 2 parameters, which I control from my script, and they work really well, but the only problem is that the animation plays for all the instances of that object at the same time.

The script I use is this one:

 private var animator : Animator;
 
 static var jump: boolean = false;
 static var attack: boolean = false;
 
 function Awake () 
 {
     animator = GetComponentInChildren(Animator);
 }
 
 function LateUpdate()
 {
     animator.SetBool("Jump",jump);
     animator.SetBool("Attack",attack);
 }

And the two static variables I set in other scripts.

The problem is that I get the animator component from that specific instance, and the parameters are set to all of them.

Anyone with a clue?

Comment
Add comment
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

1 Reply

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

Answer by AndyMartin458 · Dec 10, 2013 at 06:09 PM

A static variable will be read by all instances of that class. If you have the same script on all of those objects, and that script has a static variable, all those objects share the same variable. Try making the variable not static instead.

Comment
Add comment · Show 3 · 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 guitarxe · Dec 10, 2013 at 06:20 PM 0
Share

To add to this, if you are using static variables so that you can call them from other scripts, then try using events ins$$anonymous$$d.

avatar image Braian Correa · Dec 10, 2013 at 09:52 PM 0
Share

You are correct. I used this without the static variables, and it works individually. Thank you a lot for that! :D

But, what would you guys suggest me to do if I have an object, whose animations are triggered by collisions with the player. If the player touches one trigger, this object plays the animation "Jump", and if he touches another trigger, it plays the "Attack" one.

The thing is, how do I detect this in diferent areas and set the animator's parameters without the static variables?

avatar image AndyMartin458 · Dec 11, 2013 at 12:17 AM 0
Share

Here is some guidance.

You can use the OnTrigger function on the object to detect that the player is touching it. Store a reference to the player in the objects, or get a script off of the player from the collider other reference that is passed to OnTrigger http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html

Using that reference, set a variable on the player to count how many objects he is touching. Look at the variable in the collision and set the appropriate variable.

Something like this (pardon the C#, I don't know JS)

     void OnTriggerEnter(Collider other)
     {
         YourPlayerScript yps = other.gameObject.GetComponent<YourPlayerScript>();
             
         if(yps != null)
         {
             yps.varible++;
             if(yps.variable > 1)
             {
                 attack = true;        
             }
             else
             {
                 jump = true;        
             }
         }
     }
         
     void OnTriggerExit(Collider other)
     {
         YourPlayerScript yps = other.gameObject.GetComponent<YourPlayerScript>();
         
         if(yps != null)
         {
             yps.variable--;
             attack = false;
             jump = false;
         }
     }

 

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

18 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

Related Questions

Animation Events and Mecanim 0 Answers

What is the proper way to wait for an Animator Controller to update? 1 Answer

Can't set animator bool to True 1 Answer

C# Help with setting up jump animation 0 Answers

Problem adding individual clip speed 2 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