Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 abhishekabz004 · May 19, 2015 at 08:56 AM · c#animationinstantiateprefabchange

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 :)

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

2 Replies

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

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.

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 abhishekabz004 · May 21, 2015 at 06:13 PM 0
Share

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?

avatar image barbe63 · May 21, 2015 at 08:32 PM 0
Share

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.

avatar image abhishekabz004 · May 22, 2015 at 06:33 PM 0
Share

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 :)

avatar image
2

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

Comment
Add comment · Show 6 · 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 barbe63 · May 20, 2015 at 06:26 PM 0
Share

How comes a question becomes an answer?

avatar image JigneshKoradiya · May 20, 2015 at 06:27 PM 0
Share

what ? i dont understand

avatar image abhishekabz004 · May 20, 2015 at 06:32 PM 0
Share

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.

avatar image barbe63 · May 20, 2015 at 06:54 PM 1
Share

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.

avatar image abhishekabz004 · May 20, 2015 at 07:02 PM 0
Share

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?

Show more comments

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

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


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