- Home /
Assigning Default References to a script itself versus a GameObject using that script?
Hi, this is my first attempt to dive deep into Unity so forgive me.
I am trying to understand the conceptual difference between assigning a prefab to a property on a script which is attached to a GameObject (e.g. the main camera), versus assigning this prefab to the property on the script itself (as viewed in the Inspector under Default References).
I ask because I was stuck for a while, confused that my prefab reference in my script was null since I failed to assign it to the proper place. What does it mean to drag a prefab (or anything) to a Default Reference of a script in the inspector?
Answer by Bunny83 · Jul 31, 2011 at 10:01 PM
It's just the default reference. The default value is normally null but when you assign something to the default reference that value will be used when you create an instance of that class. That's why the default references only support Unity reference-types (everything that's derived from UnityEngine.Object as far as i know).
I'm not sure if those references will be used when instantiating objects from code, but in the editor it will work that way.
I just had a go at using default references to prefabs when you use AddComponent, and no, sadly, the defaults are not loaded in.
There goes my afternoon.
Your answer
Follow this Question
Related Questions
Public variables null after assigned in the inspector. 3 Answers
Can I access a script referenced in another script (without getcomponent)? 5 Answers
[SOLVED]Get variable of an object that creates different object with a script 1 Answer
Prefab, referencing them, using their methods:bug or intentional? 1 Answer