- Home /
Disable/Enable game object by clicking instantiated prefab during runtime
I have to create a button on a prefab that is instantiated during runtime. The button has to disable/enable a UI gameobject called "UCP", that is disabled when prefabs are instantiated. I cannot link a object in scene with a prefab (through the inspector, dragging the object) , so I tried to find the UI element from a parent (for example Canvas) with transform.Find(),after the prefab is instantiated. However I get an error "NullReferenceException: Object reference not set to an instance of an object."
On prefab:
void Start()
{
GetUCP getter = new GetUCP();
CannonPanel=getter._getUCP();
}
void OnMouseDown()
{
if (CannonPanel.activeSelf == false) CannonPanel.SetActive(true); else CannonPanel.SetActive(false);
}
On parent of the disabled game object:
public GameObject _getUCP() { return transform.Find("UCP").gameObject; }
What am I missing? please help!
Your answer
Follow this Question
Related Questions
Finding a prefab in after respawn 1 Answer
Affect All Instances Of A Prefab 3 Answers
Prefab problem, wont accept gameObject in var 3 Answers