- Home /
The prefab you want to instantiate is null.
This is driving me insane. I've looked at multiple tutorials and the Unity documentation for instantiation.
I just want to instantiate an object when I call it's function.
The documentation tells me that I must create an empty game object in order to link the script to a prefab. So I've done this and added my Script that Instantiates the prefab and the prefab itself to this empty object.
Then from another script I call the script that Instantiates the object like so:
new SpellObject();
My SpellObject code does this:
public class SpellObject : MonoBehaviour {
public Transform spellPrefab;
public SpellObject(){
Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
}
}
It should add this prefab:
I don't see how the prefab is null; it is clearly in the list with all my other scripts and items.
Ok so the script now has an option to add a transform that wasn't there before, so I've added it there and deleted the empty game object. This has no initial errors and I can run the program, but as soon as I call the script, I get the errors pop up again:
public SpellObject(){
Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
}
You should not be implementing a constructor in a monobehaviour.
Use the given function callbacks for initialization (Awake(), Start()).
@Cereal_$$anonymous$$illa As others have stated/implied, you cannot use the new keyword to create objects of classes that inherit from $$anonymous$$onoBehaviour. Trying to place a constructor in the Start function will not help you. A class that inherits $$anonymous$$onoBehaviour (e.g. SpellObject : $$anonymous$$onoBehaviour) defines a component for a GameObject, therefore it does not make sense for it to be created on its own, so Unity won't let you.
If you want objects where you can use the new keyword, they must not inherit from $$anonymous$$onoBehaviour.
If I remove $$anonymous$$onoBehaviour then I cannot use Instantiate. And I do want to create a brand new GameObject with no properties then add to it.
Would it be best to create a GameObject with hundreds of empty properties and disabled Components, and then set/reactivate them depending upon player choice?
Also I would like to be able to create more than one at a time, so should I create children rather than new copies of the original?
Answer by samizzo · Jan 18, 2015 at 10:28 AM
You can't allocate a new MonoBehaviour class using 'new', just like the Unity error is telling you. I'm not sure exactly what you're trying to do. Get rid of that SpellObject class. From the script where you want to create your spell prefab, make a property like so:
public GameObject SpellPrefab;
Then in the editor assign the SpellPrefab prefab from the asset browser to that property. Then instead of doing 'new SpellObject' do:
Instantiate(SpellPrefab, Vector3.zero, Quaternion.identity);
Thanks for the help. I might take a step back from my project and try to learn from scratch the best way to organise my project and lay out my code with unity.
FYI what I was trying to do was to create a new empty GameObject from code, then add Components and properties to the object depending upon player choice.
Like cooking - when the player gets out a bowl and starts adding flour and sugar, I don't know if it will be a cake or muffins or something else, so I wanted to just start with an 'empty bowl' so to speak.
I shouldn't have to create and link objects and scripts in the inspector - I think this confuses the issue.
Hence, I did want to instantiate a 'null/empty' game object, then modify and add to it. Is this possible? Or would it be best to create a GameObject with hundreds of empty properties and disabled Components, and then set/reactivate them depending upon player choice?
Yes that's definitely possible. Let's say you have your 'init' script on some GameObject you placed in the scene. You can then do this:
GameObject go = new GameObject("someObj");
SpellClass newComponent1 = go.AddComponent<SpellClass>();
SomeOtherClass newComponent2 = go.AddComponent<SomeOtherClass>();
That will create a new GameObject, give it a name "someObj", and add two components to it. The call to AddComponent also returns a reference to the newly added component (newComponent1 and newComponent2). You can then configure the new components as you like.
However, if you are creating them dynamically then the properties on those new components will be at their default values. You can't configure their values in the editor.
I think you might need to take a step back and work through some Unity tutorials to get a better feel for the workflow, and think about how you might want to structure your project. You'll probably want to use prefabs somewhere along the way.
Thanks for the advice. So the GameObject will be called "someObj" within the inspector, but if I want to refer to it in code, it's actual name is "go"?
Thus the inspector name is more of a description?
Also if I want to create multiple go's, can they be named go1, go2, etc automatically depending on how many are created if I want to refer back to them later? Or can I not dynamically generate variable names. Is there a better way to keep track of different instances like an ID number?
If you could recommend a good tutorial or book on designing a project from a to b with good class use, I'd be really appreciative.
Yeah the name of the object in the hierarchy will be "someObj" but the variable name in code is "go". But that could be anything else, and you could refer to it by another name later if you have another reference to the same GameObject.
You could keep track of the GameObjects that you create in an array or a list. You can dynamically create as many as you want but it would be a bit unwieldy to create hundreds of variables called go1, go2, go3 etc.
It sounds like you should do some C# tutorials too, but I don't really know of any, sorry. There's a lot of stuff on the Unity web site though. $$anonymous$$aybe take a look through there.
Answer by jenci1990 · Jan 18, 2015 at 07:34 AM
If you create a new SpellObject(), the spellPrefab is null! Use parameter when create a new SpellObject:
new SpellObject(spellPrefab);
and:
public class SpellObject : MonoBehaviour {
public Transform spellPrefab;
public SpellObject(Transform _prefab){
spellPrefab = _prefab;
Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
}
}
I've copied this directly, and created a new "public Transform spellPrefab;" in my calling class, but there is no place to assign the Transform to this script like before..