Using same animator for multiple game objects
I just started developing a 2d isometric game, and i am having a very hard time figuring out how to setup an animator. So i have a TreeChop script which fires an animation when i press a button, when i am inside the tree collider (the sprite tree is replaced by a stump). But i have a problem with it, the same animator is attached to all the trees(i have 8 of them) and when, for example i am in the collider of the first tree, and i press the chop button, some of the other trees animations are fired randomly( not even all of them:(( ) and i don't know how to resolve this problem.. I am very new to unity, just started a month ago, so if i made obvious mistakes please don't yell at me very hard:)))
public class TreeChop : MonoBehaviour
{
private Animator anim;
public GameObject cbutton;
public Button chopbutton;
public GameObject pbutton;
public Button plantbutton;
int i;
void Start()
{
anim = GetComponent<Animator>();
i = 0;
bool stop = false;
anim.SetBool("stop", stop);
}
IEnumerator waiterC()
{
GetComponent<Collider2D>().enabled = false;
chopi();
yield return new WaitForSeconds(0.7f);
GetComponent<Collider2D>().enabled = true;
}
IEnumerator waiterP()
{
GetComponent<Collider2D>().enabled = false;
planti();
yield return new WaitForSeconds(0.7f);
GetComponent<Collider2D>().enabled = true;
}
public void chopi()
{
bool stop = true;
anim.SetBool("stop", stop);
anim.SetTrigger("play");
stop = false;
i = 1;
}
public void planti()
{
bool stop = false;
anim.SetBool("stop", stop);
anim.SetTrigger("plant");
stop = true;
i = 0;
}
void OnTriggerStay2D(Collider2D player)
{
bool stop = false;
if (player.tag == "FarmerWood" && i == 0)
{
cbutton.SetActive(true);
chopbutton.onClick.AddListener(() => StartCoroutine(waiterC()));
}
if (player.tag == "FarmerWood" && i == 1)
{
pbutton.SetActive(true);
plantbutton.onClick.AddListener(() => StartCoroutine(waiterP()));
}
}
private void OnTriggerExit2D(Collider2D collision)
{
cbutton.SetActive(false);
pbutton.SetActive(false);
}
}
*this script is attached to ALL the trees, and ALL the trees share the same animator The IEnumerator functions are used for disabling the collider when the animation is fired because it causes the buttons to glitch if it is enabled. The small collider is for preventing the player to go through the tree, and the bigger one is the triggered one.
Your answer
Follow this Question
Related Questions
Confused about animations 1 Answer
Animator Check the end of an animation 1 Answer
Animator is not firing transitions. 0 Answers
NEED HELP ANIMATING PLAYER MOVEMENT 0 Answers
Problems with using the same Animator Controller on multiple objects. 0 Answers