Why can't i change the boolean?

I wanted to change the boolean by using GameObject.GetComponent<>, but the boolean did not change.
I do not know where the issues are because it worked at first. But I always think the problem is with the coroutine, because yield return new WaitForSec(5f) in the script is also not executed.

public class ObjMakeNoise : MonoBehaviour
{
    public Transform newNav;

    public AudioClip Noise;

    public GameObject Granny;

    public virtual IEnumerator MakeNoise()
    {
        GetComponent<AudioSource>().PlayOneShot(Noise);

        Granny.GetComponent<GrannyEnemyAI>().GrannyHear = true;

        if (GameObject.Find("TempNav(Clone)"))
        {
            GameObject.Find("TempNav(Clone)").transform.name = "TempNav(Clone)Old";
            Instantiate<Transform>(newNav, transform.position, transform.rotation);

            yield return new WaitForSeconds(0.5f);
            Destroy(GameObject.Find("TempNav(Clone)Old"));
        }
        else if (GameObject.Find("TempNav(Clone)Old"))
        {
            Destroy(GameObject.Find("TempNav(Clone)Old"));
            Instantiate<Transform>(newNav, transform.position, transform.rotation);
        }
        else
        {
            Instantiate<Transform>(newNav, transform.position, transform.rotation);
        }

        yield break;
    }
}

Your goal can’t be to make a noise and to look for, rename and destroy stuff based upon a transform name right? Perhaps describe what you are trying to accomplish.

The purpose seems to have been lost by all the jumping through hoops to get it to occur.

First, remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: Regarding GameObject.Find ¡ UnityTipsRedux

More information: Why cant i find the other objects? - Unity Engine - Unity Discussions

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.

If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

“Stop playing ‘Where’s GameWaldo’ and drag it in already!”

Here’s why all this stuff is CRAZY code:

Second, NOTHING needs to be a coroutine there: you’re doing everything in one frame. If you don’t understand that, hurry to a coroutine tutorial.

Coroutines in a nutshell:

Our very own Bunny83 has also provided a Coroutine Crash Course:

Coroutines are NOT always an appropriate solution: know when to use them!

“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums

2 Likes

Try sprinkling Debug.Log through the code to see how far it gets. It’s likely getting snagged on the GameObject.Find since those are extremely prone to failure (and they’re incredibly slow for the record).

I lul’d and I second this.