Why isn't it switching the fireOn state to true
I want this script to make two objects switch which one is active.
The only thing that happens is that it says true in the debug thing. They are both in the same position in game just that fireOn
starts deactivated.
I'm sorry if it's obvious but I'm new to unity.
public class FireActivateAndDeactivate : MonoBehaviour
{
private bool fireIsOn = false;
[SerializeField] GameObject fireOn;
[SerializeField] GameObject fireOff;
[SerializeField] float speed = 2f;
private void Start()
{
InvokeRepeating("TurnOnAndOff", speed, speed);
}
private void TurnOnAndOff()
{
if (fireIsOn == false){
fireOn.SetActive(true);
fireOff.SetActive(false);
fireIsOn = true;
}
if (fireIsOn == true)
{
fireOn.SetActive(false);
fireOff.SetActive(true);
fireIsOn = false;
}
Debug.Log(fireIsOn);
}
}
The reason this code is not working is this:
if
at line 16 passes, becausefireIsOn
isfalse
fireIsOn
changes it's value totrue
at line 19if
at line 20 passes, becausefireIsOn
istrue
nowfireIsOn
changes it's value tofalse
at line 25
So you either need else if
or do this:
void TurnOnAndOff ()
{
fireOn.SetActive( !fireIsOn );
fireOff.SetActive( fireIsOn );
fireIsOn = !fireIsOn;
}
Your answer
Follow this Question
Related Questions
Trying to Invoke method: Spawner.SpawnObject couldn't be called. 2 Answers
(SOLVED) Destroy(gameObject) doesn't destroy the gameObject, only disables behaviours. 0 Answers
Progressbar not working properly on click. 2 Answers
Set object from array to active only whilst a specific number is returned 2 Answers
Canvas not de-activated when played in APK (works in unity editor) 0 Answers