SystemEvent Prefab's GameObjects not working - unity3d

I have Button Prefab. It's OnClick attached to EventSystem Prefab and select method called ( doIt ).
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
Panel Prefab is inactive
I draged these Prefab ( Button, EventSystem and Panel ) in some scenes (about 17 scenes) like this.
The problem is:
When i play the game there is nothing happen when i click button
, but the panel become enabled just after i stop the game
Note:
This happened only when the SystemEvent instance depend on prefab , but if i draged the panel manually from the scene
and the SystemEvent instance to the OnClick this not happened
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.

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.

Related

Cant drag global gameObject into new unity nested prefab system

Im just starting to use the new Unity nested prefab workflow (or what ever you call it) in Unity 2018.3
So how it works is when you want to edit a prefab, you select it then click on the arrow. When you do this it erases everything from the hierarchy window and then you only have that prefab visible until your done editing it, you click save and then its back to the original hierarchy that was there before you started editing.
This is all fine, but the problem is what if i want to drag a game object from my scene into the prefab im editing is now impossible because as soon as you start editing the prefab, it removes your scene and only shows the contents of the prefab. So you cant drag anything from your scene into the prefab any more because its gone for the duration of the editing.
I dont know if im making myself clear or not.... But basically im saying while editing the prefab, only the contents of the prefab is visible, your scene is not visible, so you cant drag any object from your scene into the prefab anymore.
Anybody knows how to get around this? Right now i want to drag a gameObject from my scene into a prefab im editing to make a reference to a global object, but while editing the prefab my scene is not visible, so i cant do it.
Thank
Just create a copy of your prefab in Hierarchy and make you required changes to that copy once done, drag and drop that copy on you prefab and it will be updated.
When dropping the copy on your prefab you can either create a new prefab or update the original one.
Editing a prefab can be done in multiple ways. To achieve what you want, you should drag an instance of the prefab into your scene where your "other" gameobject is. Ensure that the prefab instance is blue, so that you know that it is still connected to the prefab.
Now, drag your "other" gameobject onto the prefab instance, so that it becomes a child of it. Now you should be able to apply prefab changes through the inspector.
Do note, if you cannot change the position in the hierarchy of the "other" gameobject onto a prefab from within the scene view. If you want to change the position, you should open the prefab scene after applying changes.

Instantiate original Prefab based on an already existing Prefab in game

I am using Prefabs for game levels.
To debug a level I just drag the prefab into Hierarchy view and press [play].
To test level I need an option to replay the prefab, but since I drop it into Hierarchy I need a way to grab the original prefab and Instantiate it again. (and destroy current active prefab).
To find current Active Prefab on Play I just use:
GameObject gameObj= GameObject.FindWithTag("Level");
How do I get the original Prefab and Instantiate it again when pressing 'REPLAY' (button to reload prefab).
I did try to use GetCorrespondingObjectFromOriginalSource
https://docs.unity3d.com/ScriptReference/PrefabUtility.GetCorrespondingObjectFromOriginalSource.html
This will always return null.
Objects in the scene don't have references to the prefabs they come from at runtime. Unity breaks these references when the scene is run (or built). PrefabUtility is an editor-only class for writing editor code.
If you want to do something similar, I'd suggest creating another script that has a reference to the prefab you wish to spawn in. This script can then instantiate that prefab in as a child when the game starts (in Awake or Start) and could have a method on it for destroying the instance and instantiating it again when you want to restart your level.

Cannot attach GameObject to a button's On click enviroment within a prefab. Unity

So I have a prefab which containcs another prefab with a group of buttons. So in one of the buttons I want to attach on the onClick enviroment a gameObject which is in my scene to get its scripts and finally its methods. I can attach just the script but I cannot find the methods of it(They are public). I can't look into the prefab and at the same time at the game object so that I drag and attach into the onClick button. When I click one the other disappears. Any suggestions?
Here is the picture of the prefab that containts the prefab of the group of buttons. Which has the UI Script (that I dragged and dropped) but doesnt have its functions(Public).
And here is the Canvas(Game object) which has the Script on it (UIManager) that I want the button to get its methods from. But when I click to canvas the prefab dissapears so I cant drag and drop it.
Thank you...
Right click on the Inspector tab label and select Add Tab -> Inspector. Drag the second inspector below the first. Click on your first object, and then click on the little padlock icon in the upper-right of the first Inspector. Now click on the second object. You can now drag-and-drop things between Inspectors.
You can attach objects to the instance in the Hierarchy, regardless of the Hierarchy instance being a prefab. You may want to approach this with some thought as to what constitutes a prefab (read: template) and what is just an object living in the Hierarchy.
You can't attach Scene objects to the prefabs in your Assets.

Why isn't Unity allowing me to apply certain changes to a prefab?

I have a prefab called square in the asset tray, I drag it into the scene to create an instance of the square prefab (in the hierarchy it is highlighted blue and has the select, revert and apply buttons in the properties). If I move/resize the instance and then press apply, the changes are applied to the prefab.
However, the square prefab has a script with a public game object. I drag the game object from the hierarchy into the slot in the instance's properties tab and is shows that the script is now referencing the actual gameobject. However, when I press apply this change isn't applied to the prefab (the prefab's script still isn't referencing any actual gameobject). All other changes to the prefab are applied.
I have tried dragging the instance into the asset tray to create a new prefab with the changes, but, as soon as I do, the script no longer references the game object.
I shouldn't have to create an entirely new prefab every time I add a public variable to a script so why can't I apply this change?
Ok you can't really drag an instance to a prefab. As a rule of thumb keep instances with instances and prefabs with prefabs.
I would instead find the instance from the prefab when this one is instantiated, with a method like findobjectoftype, or gameobject.find. Let me know if you want me to expand the answer
Solved: Prefabs can only reference other prefabs since there might not be an instance. Made the actual gameobject into an instance of a prefab and applied the prefab to the script
Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.

UI prefabs: buttons does not retain onclick() settings

I have a simple prefab, made by a prefab button and a panel.
The button is placed inside the panel so it is a child of the panel. I did save the prefab and all is good.
Then, I did add a click event on the button; so I did add the gameobject where the script that I want to use is placed, and select the correct method to use. I did update the prefab and deleted it from the scene.
Now, when I instantiate this prefab in the scene with Resources.Load("myUIprefab"); it show correctly the panel, the button and the colors/settings.
But there is nothing connected to the click button itself; which result in the button doing nothing.
Isnt' a prefab, a way to save an object to be used at later time? In that case it should retain the click settings. Is this a bug or an unfortunate implementation of the new Unity UI system?
Makes little sense to make a prefab out of a button or another GUI element, if then the click() or other delegate are not filled.
GameObjects that are stored in a prefab can only hold references to other objects inside that same prefab, or to other prefabs. This is true for object references, events, whatever.
If you want the event to persist, you must have an event handler in the same prefab and have that called by your button. This can be a simple handler method that raises a standard .Net event when called (.Net events may be easier to use than Unity events).
Sorry I'm late to the party. I managed to keep the event data setup correctly by just keeping an instance of the prefab deactivated on the scene that had the correct settings, and then pointing the behaviour that spawns them to this object rather than the prefab.
In a case, you want to call a method defined in the script attached to an unrelated game object (for example GameManager) you have to make this other game object as prefab and assign an onclick method to this prefab.