- Home /
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?
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.
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.
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?
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
![](https://koobas.hobune.stream/wayback/20220613123857im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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