Prefabs holding variables,How to carry accross game objects to prefabs
I’ve made a game where an enemy prefab is spawned and uses an empty game object as its fire point and then rotates toward the player. It instantiates a bullet I made and fires it toward the player. However, the script needs a reference to the player and camera in variable form, so I did that, but whenever I make it a prefab it loses the variable’s value and returns the error: “missingreferenceexception: the variable “player” of (script name) doesn’t exist anymore.” I hope someone can help me.,
Answer by streeetwalker · Apr 01, 2020 at 01:15 PM
HI @EwanRulez369, You've just discovered Prefabs do not retain references to other objects. You have to add the references to your null variables after the prefab is instanced.
For example:
GameObject go = (GameObject) Instantiate( prefab );
PrefabScript ps = go.GetComponent<PrefabScript>();
ps.targetTransform = myTargetTransform;
ps.camera = myCameraReference;
// and so on....
We've all gone through the same thing!
Your answer
Follow this Question
Related Questions
Variables in a script on a prefab do not store runtime changes 0 Answers
Variable changing from other script does not work. 1 Answer
How to fix "not a prefab scene"? 1 Answer
Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos' 1 Answer
Cant Change lighting/scene variables,Cant Change Lighting Properties 0 Answers