0
votes

I have Button Prefab. It's OnClick attached to EventSystem Prefab and select method called ( doIt ).

enter image description here

This method (doIt) print message and enable panel called ( Panel).

public void doIt()
{
    print("Doing !!!");
    helpCanvas.SetActive(true);
}

EventSystem Prefab has script which has method ( doIt ) and gameObject ( Button ) already attached to it

enter image description here

Panel Prefab is inactive

enter image description here

I draged these Prefab ( Button, EventSystem and Panel ) in some scenes (about 17 scenes) like this.

enter image description here

The problem is: When i play the game there is nothing happen when i click button

enter image description here

, but the panel become enabled just after i stop the game

enter image description here

Note: This happened only when the SystemEvent instance depend on prefab , but if i draged the panel manually from the scene enter image description here and the SystemEvent instance to the OnClick this not happened enter image description here

Why this happened ? and how to solve that ? It doesn't suit me or to any one to reattach the Button OnClick EventSystem Manually for all buttons which has the same function. Or to attach Panel instance to each EventSystem Instance.

1

1 Answers

1
votes

Prefabs cannot have references to objects within a scene. This is because every time Unity instantiates the prefab, how is it supposed to know if the object referenced is still there. The workaround is to add a script component on your button prefab, and then create a function inside that which preforms your desired actions. Your button prefab can have a reference only to things which are local to it (not scene dependent, will be there every time the prefab is instantiated). After you instantiate the object, your master script can do buttonInstantiatedPrefabName.GetComponent().evsys = evsys; This way, all your editor references will be stored in a master script, which will assign it to other gameObjects which are instantiated from prefabs.