Update() function keeps running AFTER script is disabled...
Hello,
I am VERY VERY VERY new to programming. I have this code (C#) that makes the word YES (UI Text) have an arrow blink next to it, like this.
YESYES
YES
And when you press the right arrow key, the arrow moves to the NO text and keeps blinking. But the YES wont have the arrow anymore. Its hard to explain...
Anyways, heres the code for the YES selection. Theres two scripts.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class yesscript : MonoBehaviour {
private Text myGuiText2;
// Use this for initialization
void Start()
{
myGuiText2 = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
myGuiText2.text = "YES";
gameObject.GetComponent<IsYesOn>().enabled = false;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
myGuiText2.text = "> YES";
gameObject.GetComponent<IsYesOn>().enabled = true;
}
}
}
and
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;
public class IsYesOn : MonoBehaviour {
Text flashingText;
void Start()
{
//get the Text component
flashingText = GetComponent<Text>();
//Call coroutine BlinkText on Start
StartCoroutine(BlinkArrow());
if (gameObject.active == false)
return;
}
//function to blink the text
public IEnumerator BlinkArrow()
{
//blink it forever. You can set a terminating condition depending upon your requirement
while (true)
{
//set the Text's text to blank
flashingText.text = "> YES";
//display > text for 0.5 seconds
yield return new WaitForSeconds(.5f);
//display nothing for the next 0.5 seconds
flashingText.text = "YES";
yield return new WaitForSeconds(.5f);
}
}
}
So after the top script disables the bottom one, it should stop blinking and I want it to stop the coroutine. But when the user presses the left arrow again, I want the entire bottom script to be enabled again, ALONG with the coroutine....
Thanks for your help! Sorry again, Im pretty new to this.
(I used some bits and pieces of others scripts online I found, I am just trying a "training" project!!!)
Answer by Johnnya101 · Feb 03, 2017 at 12:06 AM
Oops, the arrow isnt showing up.
Like this:
YES (Right arrow) YES YES