- Home /
Why is instantiated animator prefabs are not working properly?
Hi! Let me get straight to the point. So, I have instantiated an animator which is in a prefab. According to my requirements the prefabs would be instantiated multiple times in the screen. All I am trying to do is change state of the animated objects running when certain condition gets accepted.
I have done the coding part for this "state change"stuff. Here is my code.
public class changeState : MonoBehaviour {
void Update () {
Animator anim = GetComponent<Animator> ();
if (playagain == true) {
playagain = false;
string startAnimationState = "StartState";
anim.Play(startAnimationState, -1, 0);
}
}
}
The thing is this code fulfills my requirement partially.
When multiple instances of the animator is present and when the condition gets true, only the first instantiated clone is working properly. Other remains same.
I am just a beginner. So please help me in correcting my mistakes if any or if there is any replacement path.
Thanks in advance :)
Answer by barbe63 · May 21, 2015 at 02:42 AM
Oops i think I didn't understood all you said correctly. If as I think now all of your instantiated object need to change their behavior with a single call it's actually better to do it with a static bool but add another one like this:
if (alreadyPlayed == false && staticBool == true) {
//in this example staticBool is the static bool you need to change in anotherScript
alreadyPlayed = true; // this one only prevents the code to run over and over again if in an update, so it's better to set it false in the declaration
string startAnimationState = "StartState";
anim.Play(startAnimationState, -1, 0);
}
There might be better approachs to do this but this one should works and again it depends on the situation.
Thanks for replying :) This would definitely work. :) But only once. In my situation, I need to have that static bool value changing again and again. So, each time the condition gets accepted I want the clones to change their state. Do you have any suggestions for this problem?
Yes i do but i don't master it yet so I hope I'm doing it right.
I think the key is to set it with a get set method like this:
static bool _staticBool;
static bool staticBool
{
get{return _staticBool;}
set
{
if(value==true)
{
//animations stuff
}
else
{
//get back to normal
}
_staticBool = value;
}
}
This way everytime you set the staticBool value it will execute whatever is inside the set brackets.
And it is way better than the above method since you avoid putting it in an update loop and checking an extra bool.
Note also you have to use another bool (which I called _staticBool) just to avoid errors for infinite loop when you just need to read the value.
But the problem remains the same in my situation if I use static. Btw, I solved the problem in some other way :)
And I am marking this as correct answer as it would work in general situations. Thanks for helping me. :). Cheers :)
Answer by JigneshKoradiya · May 20, 2015 at 06:17 PM
if (playagain == true)
playagain bool is static bool ??
if your playagain bool is static ,when game start first instantiate object make that bool false after first time animation play,so when another object is instantiated than it has already bool=false so it not play animation
Jignesh$$anonymous$$oradiya. Yes playagain is a static bool :)
I don't think the problem would be in the code. If there is code error, then no instance should have worked. But 1st clone is working properly. Its just the other clone instantiated after the first one.
Just saying it should be in comments and not in answers.
But it seems you are right since a static bool will belongs to the class and not to the instance, the first time you put it false, it will be false for all your other objects. Just remove the static keyword.
barbe63 Thanks!! now I could see what went wrong :)
but I need them to be static as I would be changing them in other script. Could you help me in this one?
Your answer
Follow this Question
Related Questions
Problem with prefab parent and children objects that need to be instantiated multiple times. 1 Answer
Destroying a instantiated prefab? 2 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer
Only instantiate if clear space beside prefab 1 Answer
My prefab isn't getting destroyed 1 Answer