• 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
Question by sWW · Sep 19, 2014 at 10:34 AM · animation2dprefabspriteanimator

Setting animator parameter on a single instance of a prefab sets the parameter for all instances

I have created a Prefab with the intention of creating many instances of the same GameObject. However whilst I would like all of these instances to use the same Animations and Animation Controller, I want to be able to control the state for each instance independantly.

My Prefab currently consists of a Sprite Renderer, Script, Box Collider 2D and Animator. The Animator has an Animation Controller which has two states that are controlled using a boolean parameter.

The Script contains the following code:

 private Animator _animator;
 private bool _isAlive;

 private void Start()
 {
     _animator = gameObject.GetComponent<Animator>();
 }

 void Update ()
 {
     if (Input.GetMouseButtonDown(0))
     {
         var hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
         _isAlive = hit.collider == null;
     }

     _animator.SetBool("isAlive", _isAlive);
 }

The Animations are both created using sprite sheets.

So in my example I would like the Animator to only animate the GameObject that has been clicked. The click detection part works fine but all instances of the GameObject animate when any GameObject is clicked.

Comment

People who like this

0 Show 1
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 CaffeineAndCoffee · Jul 31, 2017 at 05:33 PM 0
Share

@sWW Have you solved this problem?

3 Replies

  • Sort: 
avatar image

Answer by Jor02 · Jul 31, 2017 at 06:12 PM

i think you could use the function OnMouseClick() this function detects if you clicked on the object.

so the script will be something like this:

 private Animator _animator;
         
  private void Start()
 {
              _animator = gameObject.GetComponent<Animator>();
 }
         
 void OnMouseDown()
 {
             _animator.SetBool("isAlive", false) //you can set this to true or false.
  }
Comment

People who like this

0 Show 1 · 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 CaffeineAndCoffee · Jul 31, 2017 at 06:15 PM 0
Share

@Jor02 This still affects all instances of an animator.

avatar image

Answer by kdragos · Nov 19, 2017 at 11:09 PM

Just wondering if there was a solution to this yet? (Other than creating a new animator for each object.)

Comment

People who like this

0 Show 0 · 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

Answer by Zodiarc · Nov 20, 2017 at 07:44 AM

Since every object calls the Update method, every object detects the mouse click. You would need to select the object first or find a different way to make sure that only the object you want performs the action. See at my answer here for the selection: https://answers.unity.com/questions/1268068/how-do-i-get-objects-to-move-different-speeds.html?childToView=1268135#answer-1268135

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

How to make not smooth animation? 0 Answers

Multiple Animation Controllers 1 Answer

Avatar Mask for 2d sprites with skinning and rig 0 Answers

Object not moving after changing animator parameter 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