- Home /
prefab clones all react to animation
I've been looking into this for days and figured I'd just ask for general advice. I'm trying to have a squirrel prefab walk across the screen, collide with a trigger box, wave, then continue to walk across the screen. The problem is that there are numerous pooled clones and, when one prefab clone hits the box and waves, all of the others stop. I understand that this is because they're all using the same script and my code is messy because I've been troubleshooting but I was curious if anyone had advice on the best way to correct this issue. Ideally, I'd like each individual squirrel walk, hit the box/wave, then continue walking independent of the other squirrel clones.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class psquirrelwalk : MonoBehaviour
{
private Animator anim;
private static bool waving = false;
//public static bool complete = false;
// Start is called before the first frame update
void OnEnable()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (waving == false)
{
anim.SetInteger("State", 0);
transform.Translate(new Vector3(-4, 0, 0f) * Time.deltaTime, Space.World);
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("WaveBox"))
{
other.gameObject.SetActive(false);
Debug.Log("waved");
anim.SetInteger("State", 1);
StartCoroutine(Pause());
}
}
IEnumerator Pause()
{
waving = true;
yield return new WaitForSeconds(3);
anim.SetInteger("State", 2);
waving = false;
}
}
Answer by theunsigned · May 16, 2019 at 07:47 PM
I immediately realized my problem was that the bool was a static variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201539im_/https://answers.unity.com/themes/thub/images/avi.jpg)