I’m just learning Unity and am taking a course online, Im going a little off script and decided that instead of loading a new scene on a button press I could do the entire game by just manipulating the visibility of GameObjects within a script on an empty gameController object.
I know that within the Button inspector OnClick() AttributeParentObject GameObject.SetActive works but it just doesn’t seem flexible. I’d like to be able to do it in the GameController script.
//enables Skills components and disables all else, loads in skills properties
public void Skills()
{
Debug.Log("Skills");
GameObject.Find("AttributeParentObject").SetActive(true);
}
// enable mood components and disables all else, loads in mood properties
public void Mood()
{
Debug.Log("Mood");
GameObject.Find("AttributeParentObject").SetActive(false);
}
this works to disable when clicking the “Mood button” but doesn’t enable on “Skills button” instead I get an UnassignedReferenceException.
I’ve also tried
with GameObject AttribruteController set within the GameController objects inspector.
public GameObject AttributeController;
public void Skills()
{
Debug.Log("Skills");
AttributeController.SetActive(true);
}
// enable mood components and disables all else, loads in mood properties
public void Mood()
{
Debug.Log("Mood");
AttributeController.SetActive(false);
}
UnassignedRefferceException as well.
I just don’t know what Im doing wrong.
You should assing the game object from the Inspector and after that you should use this way to enable or disable. As far as i understand from your error, it cannot find the gameObject. I prepared an example for you. See in the attachment.

So far what I can conclude is that a disabled object cannot be referenced then SetActive.
with
GameObject.Find(“AttributeParent”).SetActive(false);
but I cannot reenable it after disabling it
If you disable a GameObject by using “GameObject.Find” function, you cannot re-enable it with the “GameObject.Find” function.
If you want to use enable and disable in full function, you should assign your game object from the inspector. By the way don’t forget to check the “Text Controller Udemy” script’s box.
I feel like I do have it assigned in the inspector under “Attribute Controller”, but it still doesn’t work, what am I missing? If it’s not already assigned then how do I do that?.
like I want to be able to turn off “AttributeParent” its an empty game object with some text children objects that I just want to stop being visible when I click on a button. Then make revisible when I click on another button.
I caught that “TextControllerUdemy” check box after posting, I had hoped that that was my problem but it didn’t fix anything.
Well to be honest it seems that i cannot help you from here now. Add me on skype; yanikebubekir
Install teamviewer and i will show how to fix your problem on your pc by connecting your pc 
That’s totally normal. Visual studio doesn’t know you’ve assigned it in the inspector. It’s just a warning, and shouldn’t stop you from compiling and running.
If your variable was empty, you would get a NullReferenceException at runtime.
bekiryanik helped me. My buttons OnClick() were referencing a script on a GameObject preset in my asset folder instead of the GameObject within my Hierarchy. I see the error, but don’t fully understand the pipeline that would cause it. Thank you bekiryanik.
1 Like