- Home /
Staged Prefab can't "Apply" after adding using Gameobject as a variable
so im practicing on making games and i never really finished a game so im not that experienced in making one especially in unity
i was making prefabs and wanted to link the Player character gameobject (which is a prefab aswell) to the enemy to use his functions but when i try to apply the changes so that everyother badguy will use it after he spawns it wont let me save the changes i made and the changed parameters stays bold (Example i captured )
at first this issue was happening while changing the badguy but right now it happens in almost every prefab especially when trying to link between to gameobjects in the inspector by a script call
(example:
(C#)
...
public GUIText Score;
public PlayerControls playscr;
...
)
im almost hopeless and because of that i cant continue most of my plans, any suggestions?
EDIT:
I noticed that i can apply any parameter now but can't only game objects that are in the scene
Answer by _dns_ · Oct 04, 2014 at 04:18 PM
Hi, that's the expected behavior ! In a Prefab, you can't reference an object that is in a scene. A prefab exist on disk, it has a path to an Asset file. An object in a scene is an Instance that exists only in memory when the scene is loaded. You can't reference something in memory from something on the hard drive (only the other way around is valid)
Imagine you could reference an instance: You have "PrefabP" that references "ObjectInstanceA" in "SceneA". Now you create a second new scene "SceneB". You drop "PrefabP" in "SceneB". How can this object instance reference an object "ObjectA" that is in "SceneA" only, and only when "SceneA" is loaded, which is not the case.
I'm not sure exactly what you need to do, but what is possible is to reference a prefab from another prefab: you can drag & drop a prefab in a property field of another prefab. For example, a Hero class prefab could have a MainWeapon property. A Hero.MainWeapon of a Hero prefab could reference an Axe prefab. Then, when the Hero prefab is instantiated in memory in a scene, Unity will automatically create an instance of the Axe prefab and reference it in the Hero.MainWeapon instance. Warning: when playing from the Editor, any change made to the Axe object will be changed in the prefab file too, so this solution is not always ideal. Also, if multiple objects reference the same Axe prefab, they will reference the same instance in memory so there may be conflicts when modifying properties. Then, you may want to Instantiate() the Axe prefab yourself during Awake() or Start() to create an independent instance.
For your case: if you need your Enemy to reference your Player in a scene: in the Awake() or Start() function of the Enemy: use FindObjectWithTag or FindObject to find the player instance in the scene and assign it to playscr.
Well, difficult to explain that clearly here, I would recommend reading docs or tutorials about prefab and instances
Your answer
Follow this Question
Related Questions
Prefab not loading in data from Inspector 1 Answer
Spawning a prefab at another object's location 3 Answers
How to associate underlying data structure to prefab at runtime? 1 Answer
Get Script from gameobject knowing only parent script 1 Answer
Changes made in prefab mode via script aren't marking the object as Dirty 1 Answer