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.
Related
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
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.
I am very new to Unity3d. I want to make an prefab that expands with a little right facing button to show other objects. I have tried giving the prefab children and then dragging it into the project window, but it didn't show the little right-facing arrow. Can anybody help?
To add components to a prefab, add it to the scene as you said you have, then drag your next object on top of the prefab in the Hierarchy window. Sometimes this places it relative to the prefab but does not actually child it, so simply drag the object now in your Hierarchy onto your prefab once more and it should fall in place as a child, you will know this worked if you now have an expand node next to your prefab.
First, make your root game object that you will convert into a prefab.
In order to add objects which are children, right click on the root object and then make the children. You can also drag other gameobjects onto the root game object to make them children of the root game object.
Finally, drag your final prefab into window where all of your files are, and the resulting prefab will have children.
If you want to change the prefab after it is made, you have to change one object that results from your prefab and after changing it, click Apply in the top of the Inspector window to apply that change to all other objects resulting from the same prefab.
My prefab will not retain the move joystick reference. If I add the reference back while running, the everything works, but the instantiated prefab will lost the reference once I delete it from the screen, or run the project.
I was sure to hit Apply on the prefab, but essentially, its acting as though I didn't.
the reference is added here...
but gone on the instantiated object...
Note that the ship object that I drag into the scene still has the reference, but the instantiated ship does not.
I've tried this with the joystick container as a prefab and not a prefab.
This is expected behavior
Prefabs cannot maintain references to objects in the scene, as when they get instantiated, there's no guaranteed that that object still exists.
You will have to assign the reference to the script when you instantiate the prefab. You can do this by calling instance_obj.GetComponent<Move>().moveJoystick = ...
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.