- Home /
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?
Answer by tanoshimi · Jul 18, 2017 at 07:03 AM
If you just want to make the object invisible, you might want to disable its renderer rather than set the entire object unactive: https://docs.unity3d.com/ScriptReference/Renderer-enabled.html
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);
Wrong a bit, should be object.SetActive (!object.activeSelf);
Thank you for your replies!
I didn't know this renderer. Disabling itself is working now, thank you.
Do you know how I can enable/disable another object like a 3D-Text with this renderer? I couldn't find a suitable solution, things like instantiatedObjectToTagAlong.GetComponent().enabled = true; did'nt work.
Answer by noscoars · Jul 19, 2017 at 12:10 PM
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.
Your answer
Follow this Question
Related Questions
Toggle button help 1 Answer
Aimdown Sights Toggle 1 Answer
Can't Get my 2nd Window to Open.. 3 Answers
Toggling multiple button states 2 Answers
Combine actions in if statement 1 Answer