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.
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
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).