- Home /
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 Animation
s 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 Animation
s 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.
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.
}
@Jor02 This still affects all instances of an animator.
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.)
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
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Change entire spriteSheet from animation tree 2 Answers
Animator - Stoping && Playing from Specific frames. 5 Answers
How to make not smooth animation? 0 Answers
Multiple Animation Controllers 1 Answer