Total Noob Alert! < 7 days with C# and Unity
My Win Game screen becomes SetActive and my Player is SetActive false when you are standing in a trigger and crouch. That all works fine except you cant see the crouch animation because he is IMMEDIATELY SetActive false.
I tried to delay this by creating a coroutine. The problem with that is this is all happening in the Update function (or whatever it's called) because of the getkeydown for crouch. That gets called once per frame so my coroutine is trying to go off once per frame.
I tried creating a bool called isCoroutineStarted and set it to false and then set it to true in the Coroutine to break the loop but that didn't work either.
Don't know what to do so I reverted it back to before where it doesn't show my crouch animation.
Here's my script (It's attached to a Game Object with the trigger):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class xxxxxx : MonoBehaviour {
public bool xxx = false;
[SerializeField]
private GameObject youWinUI;
[SerializeField]
private GameObject player;
// Determines whether or not the player is standing on the xxxxx
void OnTriggerEnter2D (Collider2D other)
{
xxx = true;
}
void OnTriggerExit2D (Collider2D other)
{
xxx = false;
}
//Turns on Win screen and makes player disappear when crouch on xxx
void Update ()
{
if (Input.GetKeyDown (KeyCode.S) && xxx == true) {
youWinUI.SetActive (true);
player.SetActive (false);
}
}
}
P.S. ignore the x's no stealsies lol. Also this piece of script works as intended I just want a delay so you can see the crouch animation.