Hello,
I’ve got an issue here, hopefully it will prove an easy one…
I have a ‘Part’ class from which some objects derive from… this Part script is never attached to a gameObject, but it extends MonoBehaviour and the scripts that do get attached extend from it.
This works… but I hit a problem…
I’m trying to instantiate explosions on these part-extended objects, and decided that giving the base class a reference to the explosion prefab would be much simpler than giving each and every part it’s own reference to it…
Since the Part base class is never attached to an object, it seemed like a good idea to set the explosion prefab reference as a ‘default reference’ for the class…
I declared an open reference for the explosion prefab, and dragged it over to the default reference field that popped up for the Part class… so far so good…
Trouble is, that when the time comes to instantiate the prefab, unity halts and says the ‘explosion’ object was never referenced 
Looking over the derived classes, I saw that they now have an inspector handle for the explosion prefab… and the default reference was completely overlooked by Unity… It just passed the reference onto the derived classes
What am I doing wrong this time?
Thanks in advance
Cheers
I do not in any way know the answer to this question. However, a class that’s derived indirectly from MonoBehaviour failing to observe a default reference associated with a base class seems like the kind of bug/oversight that could easily occur. (I’m not saying it is a bug - I’m just saying it might be.)
What I’d probably do is try to create a minimal example demonstrating the behaviour, and if you’re able to, submit it along with a bug report. (If it is a bug though, you’ll probably just be stuck with the behavior for the time being.)
How are you assigning the default? In a constructor? Static constructor? Awake()? Or via the GUI?
I’m just declaring a public variable in script. I’m trying to assign a default value to it through the editor GUI.
The base class itself isn’t attached to any gameObjects, so it only appears in the Project panel. Selecting the script there shows it’s definition in the Inspector panel, where you can see the code (or whatever part of it that fits in that little panel), and can set default references, which show in a field right below the code.
Declaring a public variable adds a new field on the default references panel, and I’m just dragging my gameObject over to that field…
Only that Unity doesn’t really assign it… it passes the empty reference onto the child classes…
I hadn’t thought it could be a bug… now that you mention it… it does seem likely…
Anyways, I’m going for something better now… I’m gonna have an instance manager script holding references to anything spawnable, and other scripts can grab a reference off that… should be even better, since it’s a single place where I can plug and unplug new assets at will. (I’m trying my best to separate the assets from the code… not so easy in Unity)
Thanks for the help!
Cheers
I am having the same issue.
Create a script which can be attached to objects and I want it to spawn a sprite effect (burning) but its coming up as “null”