2

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

But surely I should be able to just press apply. I shouldn't need a script to make a change to a prefab.
You can't link a Prefab asset with an object in the scene. Supposing you create a new scene (scene B) and you drag & drop the prefab into this scene, you would have a reference of an object located in scene A, which would not make sense.
if you try to drag a reference of an instance to the prefab. imagine , for example you are trying to instantiate that prefab in a different scene that hasnt got the instance you want to reference, what would it happen?
0

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

1 Comment

You can apply the prefab version to the prefab, but the prefab version is still an asset, not the object in the scene.
0

Prefabs are Assets. You can reference only other Assets to an Asset, never instances of Assets.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.