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.