- Home /
With GvrGaze gameObject.Find followed by SetActive(false) permanently disables object (in future runs)
This is the first time that I have felt the need to ask a question, so I hope that it is not a stupid one. I am trying to implement a simple VR "Continue", "Exit" interaction where the Exit option is followed by "Thank You". I have separate 3D objects for the options and wish to turn the "Thank You" option off at the start.
Unfortunately the code works fine ONCE but then permanently disables the object for future use with the following error message.
Awake: No Thanks Game Object UnityEngine.Debug:Log(Object) GvrGaze:Awake() (at Assets/GoogleVR/Scripts/UI/GvrGaze.cs:108)
Modifications to GvrGaze.cs
using UnityEngine;
using System.Linq;
private GameObject myContinue;
private GameObject myExit;
private GameObject myThanks;
void Awake() {
cam = GetComponent<Camera>();
PointerObject = pointerObject;
myContinue = GameObject.Find ("Continue");
if (myContinue == null) {
Debug.Log ("Awake: No Continue GameObject");
}
myExit = GameObject.Find ("Exit");
if (myExit == null) {
Debug.Log ("Awake: No Exit GameObject");
}
myThanks = GameObject.Find ("Thanks");
if (myThanks == null) {
Debug.Log ("Awake: No Thanks Game Object"); **108**
} else {
myThanks.SetActive (false);
}
// DO NOT DEACTIVATE HERE (OTHERWISE PERMANENT !)
}
Trying to deactivate in OnEnable has the same result.
Hi! Your code is really difficult to read without the code formatting option on for that block of text. Can you edit your post and turn it on? You can select the text and press the button. It's this button: 
The problem is, GameObject.FInd can't find GameObjects that are inactive. You could only call Find if myThanks is null, because when you found it once, there's no need to search for it again. Why not making the fields public and assign those in the inspector? Don't answer if they're instantiated ;)
The ThankYou object is set as active in the scene. The code then works once. The object returns to being active in the scene But on a second run the error message occurs, and the object returns as active in the scene. The error message then re-occurs on every subsequent run and there is nothing you can do with the scene to turn it on again, for instance setting it as inactive and then as active again. The only thing that works is to delete the object and recreate it., when it will then work just once.
Your answer