Hi and welcome to the unity forum.
You can use code tags to post some code in the forum (see here: Using code tags properly )
Here is why your code is not working:
public class MenuTurret : MonoBehaviour
{
public GameObject menuTurret;
public bool isActive; // this is never SET, therefore it is always FALSE
private void Start()
{
menuTurret.gameObject.SetActive(false);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartCoroutine(Toggle()); // start the coroutine whenever the space key is pressed
}
}
IEnumerator Toggle()
{
// !menuTurrent as bool is always FALSE and isActive is also always FALSE
// Note: this is not very "clean", you are comparing a GameObject to a Boolean.
if (!menuTurret == isActive)
{
menuTurret.gameObject.SetActive(true);
yield return 0;
}
// the else path is never executed (see "if" comment above)
else if (menuTurret == isActive)
{
menuTurret.gameObject.SetActive(false);
yield return new WaitForSecondsRealtime(1.5f); // this line does practically nothing. It will wait for 1.5 Sec AFTER everything is done and then return
}
}
}
And here is one of many possible solutions:
public class MenuTurret : MonoBehaviour
{
// Be aware that this will NOT work if menuTurrent is this very object.
// Objects need to be active in order to start Coroutines.
public GameObject menuTurret;
/// <summary>
/// A reference to a currently running coroutine (maybe null if none is running at the moment).
/// </summary>
protected Coroutine currentCoroutine;
public void Start()
{
SetTurretActive(true); // this activates the turret but it does not start the auto disable timer.
// Version with auto disable timer
// SetTurretActive(false); // init as disabled
// Toggle(); // then just toggle it
}
public void Update()
{
// whenever space is pressed we want to toggle
if (Input.GetKeyDown(KeyCode.Space))
{
Toggle();
}
}
/// <summary>
/// Toggles the turret. The turret will auto disable after 1.5 seconds.
/// It is useful to have this in a separate method because you may want to toggle it not only via a key press.
/// With this you can toggle it from whatever code you want.
/// </summary>
public void Toggle()
{
// First whenever we toggle let's stop a potentially running coroutine.
if (currentCoroutine != null)
{
StopCoroutine(currentCoroutine);
currentCoroutine = null;
}
// Now if we need to activate then let's start the coroutine.
if (IsTurretActive() == false)
{
SetTurretActive(true);
currentCoroutine = StartCoroutine(AutoDisableCoroutine(1.5f)); // be aware that THIS gameobject needs to be active in order for this to work.
}
// otherwise disable immediately.
else
{
SetTurretActive(false);
}
}
/// <summary>
/// Disables the turret after "timeOutInSec" seconds.
/// </summary>
/// <param name="timeOutInSec"></param>
/// <returns></returns>
IEnumerator AutoDisableCoroutine(float timeOutInSec)
{
// wait for N seconds
yield return new WaitForSecondsRealtime(timeOutInSec);
// disable after waiting
SetTurretActive(false);
}
/// <summary>
/// Returns true if the game object itself is active (does NOT take the hierarchy into account).
/// </summary>
public bool IsTurretActive()
{
return menuTurret.activeSelf;
}
public void SetTurretActive(bool active)
{
menuTurret.SetActive(active);
}
}
Happy Coding!