UI prefabs: buttons does not retain onclick() settings - unity3d

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.

Related

Can you attach a sprite renderer to a child of a main game object and still have the animator access functions from the main object for events?

When creating a player or enemy in unity, I've learnt that it's best practise to attach the sprite renderer to a child game object so that you can easily reposition the sprite. However, doing this means that the animator also needs to be attached to the child game object, which means you don't have any access to the functions from the parent game object for animation events . Does anyone know a way around this?
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
I tried putting the animator on the parent object, but then it said the sprite renderer component was missing so I believe they need to be on the same object.
No! What happened is you moved the animator root so the original property paths stored in your animation clip is invalid now.
The property paths are basically stored via the objects names.
What previously was e.g. (don't know the exact form of the property paths)
/Sprite/SomeProperty
would now need to be something like
YourSpriteObject/Sprite/SomeProperty
but since the animation still uses the first one and there is no Sprite component on your parent object it is invalid and displays as missing.
You will have to redo your animations or write an editor script for automatically changing all animation paths accordingly.
Here I once did something similar for renaming animated objects. You could probably write a similar thing for moving the animator component to a different object in the hierarchy
Alternatively expose the methods on your child object, and then forward them to trigger stuff on the parent via
transform.parent.GetComponent<YourParentClass>().YourMethod();
which of course I would either cache once or assign via the Inspector

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.

Can't apply prefab changes

i have a problem when try to apply changes.
I have a GameObject in the hierarchy, i put other GO in a custom script and push apply button. All has saved, but 2 game object can't apply canges.
Thanks in advance.
If you add new objects to the transform hierarchy, those new objects can't reference the prefab because they don't know they're part of the prefab. You can tell this because in the screen hierarchy list the prefab items are blue and the rest are not. You have to save the changes from an object that is part of the prefab first or redefine the prefab by dragging the parent object onto the prefab on the project list. Because a direct save was possible, it will overwrite.
The same is still essentially true for removing objects from the prefab hierarchy, except Unity knows that this is a breaking change (because it wouldn't be mappable to the prefab anymore) and informs you that doing so well remove ALL of the objects from the prefab upward reference and you can only save the changes by redefining the prefab by dragging it from scene to project (and possibly receive another warning about the objects being different: are you sure you want to overwrite?).
Likely those Text and Transform objects are found outside of the prefab. Prefabs can't reference external objects, since there's no guarantee they'll be available for every instantiation of the prefab.