My object desactive and active multiple time in a frame

Hello i have a very simple problem in my script, the problem is when i press my key, the fonction desactive and active multiple time, i dont understand because i use getkeydown who normally use one frame.


130664-important.png

if someone can explain the problem it be very very nice and thankful, sorry for bad english

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LightController : MonoBehaviour
{

	// Update is called once per frame
	void Update()
	{
		if (Input.GetKeyDown("f"))
		{
			if (gameObject.activeSelf == true)
			{
				gameObject.SetActive(false);
				Debug.Log("Desactive light");

			}
			if (gameObject.activeSelf == false)
			{
				gameObject.SetActive(true);
				Debug.Log("Active light");
			}
		}
	}

}

Look careful at the line where i added the comment:

void Update()
{
    if (Input.GetKeyDown("f"))
    {
        if (gameObject.activeSelf == true)
        {
            gameObject.SetActive(false);
            Debug.Log("Desactive light");
        }
        
        // You have no else here.
        
        if (gameObject.activeSelf == false)
        {
            gameObject.SetActive(true);
            Debug.Log("Active light");
        }
    }
}

Since you have no else between your two if statements, they are both executed one after another when you press your button. So if the object is active when you press the button your first if statement is true and you deactivate the object. Now the execution continues to the second if statement. Since you just deactivated the object the second if statement is also true and you activate the object again within the same frame. Your code only run once.

You should do something like this:

void Update()
{
    if (Input.GetKeyDown("f"))
    {
        if (gameObject.activeSelf)
        {
            gameObject.SetActive(false);
            Debug.Log("Desactive light");
        }
        else
        {
            gameObject.SetActive(true);
            Debug.Log("Active light");
        }
    }
}

Or even simpler:

void Update()
{
    if (Input.GetKeyDown("f"))
    {
        gameObject.SetActive(!gameObject.activeSelf);
    }
}

This will simple toggle the state since we pass the opposite of the current state as the new state (note the !).

edit

As it turns out the approach of deactivating the gameobject doesn’t work since the gameobject can not activate itself again once it’s deactivated. To toggle (enable / disable) just the Light component you can do:

private Light m_Light;

void Awake()
{
    m_Light = GetComponent<Light>();
}

void Update()
{
    if (Input.GetKeyDown("f"))
    {
        m_Light.enabled = !m_Light.enabled;
    }
}

It’s checking both ifs at once, so it’s turning off and on in a single frame. Use “else if” on the last if statement, that way it will only do the first statement that returns true and skip the rest.