Prefab loses reference when instantiated

Hello, I have a case where the player creates a hitbox for an attack, and the instantiated version of the hitbox is missing a data reference that is present on the prefab.


This is the inspector panel for the prefab
117086-prefab.png


when I instantiate the prefab, this is what I get
117089-instance.png


I load the prefab via resources.load when the game starts, and the direct reference still has the proper reference when loaded (I checked using Debug.Log when the sword is swung). Only when it is instantiated is the reference lost. Any ideas?

So after learning a a bit about serialization, Unity only serializes public variables and the list I was manipulating was private. All I had to do was put [SerializeField] before the list declaration and now it works fine. [SerializeField] forces Unity to serialize a private variable so that it will be saved as if it were public. More info: Unity - Scripting API: SerializeField

No, upon restart it does not stay. What am I missing regarding serialization? I’m sort of iffy on the concept.

Your public variables are only remembered whilst the GameObject is in the scene. When you delete it and re-instantiate it, (or just instantiate a new one) the ‘public’ vars are reset to default.
You will need to declare them programmatically, usually using the Start function.

  • .

GameObjects work pretty much the same as a Class in programming, they are blank templates that need their information parsed into them. When you edit your prefab’s public variables, you aren’t altering the GameObject itself, only that instance of the GameObject.