• 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 w$$anonymous$$ch 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 t$$anonymous$$s "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 t$$anonymous$$ng is t$$anonymous$$s 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 t$$anonymous$$nk I didn't understood all you said correctly. If as I t$$anonymous$$nk 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 t$$anonymous$$s:

          if (alreadyPlayed == false && staticBool == true) {
             //in t$$anonymous$$s example staticBool is the static bool you need to change in anotherScript 
              alreadyPlayed = true; // t$$anonymous$$s 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 t$$anonymous$$s but t$$anonymous$$s 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
avatar image barbe63 · May 21, 2015 at 08:32 PM 0
Share
avatar image abhishekabz004 · May 22, 2015 at 06:33 PM 0
Share
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
avatar image JigneshKoradiya · May 20, 2015 at 06:27 PM 0
Share
avatar image abhishekabz004 · May 20, 2015 at 06:32 PM 0
Share
avatar image barbe63 · May 20, 2015 at 06:54 PM 1
Share
avatar image abhishekabz004 · May 20, 2015 at 07:02 PM 0
Share
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

VUFORIA: Playing an animation for a instanced model? 2 Answers

instantiating vertically 2 Answers

How do I re-instantiate an object after it is destroyed from the scene? 1 Answer

Multiple Cars not working 1 Answer

Call a function in an instanciated prefab has no effect on the prefab 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