Question by
DeSterrennacht · Aug 15, 2019 at 05:23 PM ·
2danimatorsetfloat
animator.SetFloat doesn't work
So i have five gameObjects-hearts (aka representation of player's health) in an array "hp". The idea is to make the last heart disappear when enemy hits the player, and for this to happen i only need to set float "death" to 1 in OnCollisionEnter2D, but the "death" float in every heart always stays 0 (Debug.Log works though). My only guess is that i made some stupid mistake in Start function. Any thoughts?
public class health : MonoBehaviour
{
public GameObject heart;
GameObject[] hp = new GameObject[5];
Animator[] animator = new Animator[5];
int i = 0, currentHealth = 5;
IEnumerator Start()
{
for (; i < 5; i++)
{
hp[i] = heart;
Instantiate(hp[i], new Vector3(-43 + i * 2.5f, 11.5f, 0), Quaternion.identity);
animator[i] = hp[i].GetComponent<Animator>();
yield return new WaitForSeconds(0.2f);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("enemy") && currentHealth>0)
{
animator[currentHealth - 1].SetFloat("death", 1);
currentHealth--;
Debug.Log("hp " + currentHealth);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Help with 8-DOM Weapon Sprite Issue 0 Answers
Apply Root Motion Causes Animation to Not Play 0 Answers
Issues w/ 2D shmup ship "lean" animations on diagonals 1 Answer
How to create a seamless looping animation, without noticing when the loop occurs? 1 Answer
HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 1 Answer