- Home /
Help with a low health script
Hello I need help with a low health script. I have a gameobject with a health text value and I am trying to play a sound on low health. The script I am trying to use below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour {
public Text health;
public AudioSource mysound;
void Update () {
if (health=5) {
mysound.Play();
}
}
I drag my playerhealthtext into the public health field create an audiosource but I can't get it to work. Any ideas how I can implement this?
Answer by tormentoarmagedoom · Mar 19, 2020 at 10:29 AM
Hello.
I think you need to watch some tutorials before continue..
First: if (health=5) is incorrent for so many things
its not =. it must be == because is a comparation
Second, health is a text component, not a string, not a integrer so can not be equal to any number or text
Third: I understand wht are you trying, but even you solve all the porblems i told, it will be sending the function Play every frame the health is exatcly 5, every frme (this mesn line 10 times a second...) and never when is 4 or 3...
Please. Stop, take your time, bgo back to tutorials and then restart again.
Bye.
Your answer
![](https://koobas.hobune.stream/wayback/20220612225202im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
My Running sound doesn't play or sound as it should, Help 1 Answer
During sound between scenes 1 Answer
(help) Door sound 1 Answer
Randomizing sounds (on keypress)? 1 Answer