Reassign prefab in Unity - unity3d

Is it possible to reassign a prefab to a GameObject? I removed a prefab because the prefab attached script was removed and was throwing a warning. I was unable to remove the script from the inspector and save it, so I had to remove the prefab.

If they are made from the same prefab, what you can do is: Drag one object from hierarchy to "Project" as you would create a new prefab from the existing "missing" prefab.
This would ensure that they have a new parent prefab - with the same GUID as previously.
One downside would be that it revert the names of all prefabs in the scene you are currently in, to the new prefab name.
NOTE: This would not work if you closed the scene or Unity.
EDIT:
You can also modify scene manually by assigning m_PrefabInstance: {fileID: 665558973} to the same ID for all your GameObjects. GameObjects with missing prefab would have fileID: 0.
You need to do this for all components probably (GameObject, Transform, Components etc). Make sure you have backup before trying this though.

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.

How to replace model without a prefab file?

I'm new to ARCore and followed this article: https://haptic.al/arcore-101-fa6f93d4c003 to add a model with animation and see it with my phone camera.
I switched the model from the article (kitty) with a ball that I downloaded from Unity assets store, I just replaced the previous prefab object with the new one from the ball package that I imported.
Everything works fine, but now I want to download other model from the Unity store and I see that some models don't include the prefabs folder or a prefab file inside the the model package.
Is there another way to switch my current model with a new one without being use the prefab file?
Thank you.
Even if the downloaded file doesn't contain a prefab you can still create your own prefab by placing the object in the scene (.fbx, .obj files etc.) adding whatever materials you want it to have, and then dragging the name from the Hierarchy panel into the Project panel. The name in the hierarchy panel should turn blue if you've made a prefab successfully.
Note: you don't need a prefab to put something in the scene, a prefab represents an object that has a particular state saved instead of the defaults (transform rotation, materials, colliders, etc.). If you add things to an object and save that object as a prefab then whenever you place that prefab into the scene it will have those components still attached.

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.