0
\$\begingroup\$

So my problem is that the color of the sprite should only change if the Boolean is true. My theory is that the Boolean isn't being update. So how do I update the Boolean the whole time and see if it is getting changed.

Thanks Guys,

 public class controlborder : MonoBehaviour
    {
        private SpriteRenderer m_spriteRenderer;
        public float loopdelay = 2f;
        public static bool startstop;
        public void Start()
        {
            m_spriteRenderer = GetComponent<SpriteRenderer>();
            StartCoroutine(Changecolor(2f));
        }
        public void Update()
        {
        }
        private IEnumerator Changecolor(float loopdelay)
        {
            while (true)
            {
                startstop = control.changecolorborder; <== this bool
                Debug.Log(startstop);

                while (startstop)
                {
                    yield return new WaitForSeconds(1F);
                    int random = Random.Range(1, 4);
                    if (random == 1)
                    {
                        m_spriteRenderer.color = Color.blue;
                    }
                    else if (random == 2)
                    {
                        m_spriteRenderer.color = Color.red;
                    }
                    else if (random == 3)
                    {
                        m_spriteRenderer.color = Color.green;
                    }
                    else
                    {
                        m_spriteRenderer.color = Color.yellow;
                    }

                }
            }
        }
    }
\$\endgroup\$
1
  • \$\begingroup\$ This looks very similar to your previous question. Please consider editing your existing question rather than posting a new one about the same basic issue (not knowing how to reference variables between script instances) \$\endgroup\$
    – DMGregory
    Oct 9, 2019 at 9:42

1 Answer 1

1
\$\begingroup\$

I'm not sure about the intent of your code and what 'control' is in this context.

However, I think you are missing a yield return null; in the outer while(true) loop. Following your current logic, once you set startStop to false, nothing will be able to alter control.changecolorborder anymore because your code is stuck in that outer while loop constantly setting startStop to false over and over again...

I'm not sure if this is clear to you, but in case it is not, coroutines are not multi threaded and do not run asynchronously. They are run on the same thread in Unity. With yield return ...; you can halt the coroutine so that it can continue executing later (Unity decides how much (frames) later it will continue executing your method depending on what you yielded). Once you yield, your other code (Update, etc.) can run. If you never halt your coroutine (by yield returning something), your other code can't execute as it is still waiting for your coroutine to either finish or yield.

See also: the manual on coroutines.

\$\endgroup\$

You must log in to answer this question.