Having trouble with UI text please help
So I'm setting up a scene where the user receives a welcome message at the start of the game. Another message when the user collides with a trigger, and a final message after the trigger event. After each message displays I deactivate the Canvas gameObject and reactivate it for the duration of the following message. This works perfectly as intended for the first two messages however I'm having a hard time with the final message.
I know the way I have it setup now isn't working properly because I'm calling my method in Update which prevents the Canvas from ever being deactivated due to the if statement. I can't think of how else to narrow down the exact moment when the message should be displayed other than checking if any enemy tagged objects exist and if the trigger object exists, and I've tried calling the method through OnEnable subscribing it to a method from another script but it just conflicts with the execution of the first two messages.
Here's the script I'm using for the final message what am I missing? I apologize I have a few different things going on in this script.
public class FindEnemies : MonoBehaviour
{
private Color myColor = new Color(0.5f, 1f, 0.5f, 0.8f);
private Color lightColor = new Color(0.6f, 0.6f, 1f, 1f);
private GameObject[] enemies;
private GameObject[] triggers;
private GameObject myLight;
private Text textObject;
private string defeatText = "Ugh I need to get some zombies. Well good job hero you've killed some cubes";
public GameObject welcomeCanvas;
void Start()
{
SetInitialReferences();
GetComponent<Renderer>().material.color = myColor;
myLight.GetComponent<Light>().color = lightColor;
}
void Update()
{
SearchForEnemies();
}
void SearchForEnemies()
{
enemies = GameObject.FindGameObjectsWithTag("Enemy");
triggers = GameObject.FindGameObjectsWithTag("Trigger");
if (enemies.Length == 0 && triggers.Length == 0)
{
MyDefeatEvent();
GetComponent<Renderer>().material.color = myColor;
myLight.GetComponent<Light>().color = lightColor;
}
StartCoroutine(DisableCanvas());
}
void MyDefeatEvent()
{
MakeActive();
textObject.text = defeatText;
}
void MakeActive()
{
welcomeCanvas.SetActive(true);
}
void SetInitialReferences()
{
textObject = GameObject.Find("TextWelcome").GetComponent<Text>();
textObject.fontSize = 16;
myLight = GameObject.Find("DirectLight");
}
IEnumerator DisableCanvas()
{
yield return new WaitForSeconds(2.5f);
welcomeCanvas.SetActive(true);
}
}
}
Answer by SageGlaze · Feb 10, 2019 at 05:35 AM
Doh! So looking back through my script I noticed that, during my scuffle with editing things around, I stupidly wrote welcomeCanvas.SetActive(true); within my Coroutine which is just silly. This was not always the case and did not fix my issue. However I did decide trying to call my Coroutine within my if statement and that certainly did the trick! Hope this helps someone else out here is the corrected script: public class FindEnemies : MonoBehaviour { private Color myColor = new Color(0.5f, 1f, 0.5f, 0.8f); private Color lightColor = new Color(0.6f, 0.6f, 1f, 1f); private GameObject[] enemies; private GameObject[] triggers; private GameObject myLight; private Text textObject; private string defeatText = "Ugh I need to get some zombies. Well good job hero you've killed some cubes"; public GameObject welcomeCanvas; void Start() { SetInitialReferences(); GetComponent<Renderer>().material.color = myColor; myLight.GetComponent<Light>().color = lightColor; } void Update() { SearchForEnemies(); } void SearchForEnemies() { enemies = GameObject.FindGameObjectsWithTag("Enemy"); triggers = GameObject.FindGameObjectsWithTag("Trigger"); if (enemies.Length == 0 && triggers.Length == 0) { MyDefeatEvent(); GetComponent<Renderer>().material.color = myColor; myLight.GetComponent<Light>().color = lightColor; StartCoroutine(DisableCanvas()); } } void MyDefeatEvent() { MakeActive(); textObject.text = defeatText; } void MakeActive() { welcomeCanvas.SetActive(true); } void SetInitialReferences() { textObject = GameObject.Find("TextWelcome").GetComponent<Text>(); textObject.fontSize = 16; myLight = GameObject.Find("DirectLight"); } IEnumerator DisableCanvas() { yield return new WaitForSeconds(2.5f); welcomeCanvas.SetActive(false); } }