SetActive(true) is working, SetActive(false) not

Hey there

I’m new in developing C#/Unity and I have to make an app prototype for school with Unity (5.6.2.1f2) for HoloLens. I’ve completed the Gaze tutorial and this works well: holoacademy/holograms-210

So there’s a assigned object in GameObject “ObjectToTagAlong” and the code in line 28 instantiatedObjectToTagAlong.SetActive(true); works fine: When an object is deactivated in Unity, this function shows the assigned object when I click on the “main object”.

Now I have to not just do activate it, it also should be possible to hide it when clicked on the same “main object” or the assigned object itself → like a “toggle”. So I’ve tried instantiatedObjectToTagAlong.SetActive(false); which is not working to an object which has been set to visible in Unity before (the opposite procedure like the working one).

Can somebody explain me why SetActive(true) is working and SetActive(false) not? Or do you have a better solution to toggle game objects?

If you just want to make the object invisible, you might want to disable its renderer rather than set the entire object unactive: Unity - Scripting API: Renderer.enabled

However, there’s nothing obviously wrong with your approach - when you say it is “not working”, do you get any error in the console? Are you sure that section of code is even being called? You can toggle an object active/not with the following:

object.SetActive (!object.activeInHierarchy);

So I’ve found a solution which works for me:

ObjectToTagAlong.gameObject.GetComponent().enabled

Thank you very much for your supply and I hope this works for other with the same problem.