I am currently trying to get GameObjects to toggle on or off when a UI button is clicked. I have found a few different threads regarding this, but all are slightly different from my case. This is as simple as it seems to be. I pass in a GameObject reference, and if the button is clicked, I pass it to a function which switches a global boolean and passes that to SetActive(). For whatever reason, I can not get this to work, whether I start with the GameObject as active or inactive. What am I missing here?
[SerializeField] GameObject shoulderRod, kneeRod, footRod, spineRod;
[SerializeField] Button button;
bool flag;
// Start is called before the first frame update
void Start()
{
button.onClick.AddListener(delegate {
toggleRods("");
});
}
void toggleRods(string thisdoesnotmatter)
{
Debug.Log("Flag Before: " + flag);
flag = !flag;
Debug.Log("Flag After: " + flag);
shoulderRod.SetActive(flag);
kneeRod.SetActive(flag);
footRod.SetActive(flag);
spineRod.SetActive(flag);
}
Sorry, I should have been more clear. The function is being called, and the boolean value updates correctly. The function executes all the way to the end (Debug.Log can be called after where I am trying to activate/deactivate my GameObjects), my problem is quite literally is just SetActive is not working!
Assuming your gameobjects are hooked in correctly (double check just to be sure). You could write a quick little script with the OnEnable and OnDisable methods
Throw this script on those gameobjects and see if they are being turned on but then turned off again. Or just to verify if it is being turned on.
You can pass a context object to a Debug.Log call as second argument. This context object (has to be a UnityEngine.Object derived type) is then highlighted in the editor (hierarchy or project view) when you click on the log message in the console. This makes it very easy to figure out which objects you actually interact with. So add a Debug.Log and pass for example shoulderRod as context object. Click on the message and see which object you actually set active / inactive. They may not be the objects you wanted.
I think I found my solution! I was placing my script in an empty GameObject, and the ones I was trying to reference were children of a separate GameObject. As soon as I placed my script as a child of the same object, my script worked as intended. I didn’t realize how much hierarchy affected visibility. Thank you all for the input!
I had the same problem and figured it out: if you call setActive(false) then the update() method of that object will not be called anymore. I tried setting setActive(false) in start() method and then setActive(true) in update() method after some condition. The update method never ran after setting setActive(false). The sollution is to use meshrenderer which does similarly (deactivates the mesh thus hiding the object):
private MeshRenderer renderer1;
void Start()
{
renderer1 = GetComponent(); // Get the Mesh Renderer that is attached to this GameObject
renderer1.enabled = false; // Disable the renderer so that it is invisisble
}
void Update()
{
if (DigScript.finishedDigging)
{
renderer1.enabled = true;
Debug.Log("Ok now show bone " + renderer1.enabled);