- Home /
Instantiate throws NullReferenceException
This two lines are right after each other in the code. D_ReferenceCard is accessible in the following code.(not empty) The class PlayerCard they inherit from indirectly inherits from monobehaviour.
D_ReferenceCard = D_Base.getPlayerByID("1");
This works, but this:
D_Base_Card = (PlayerCard) Instantiate(D_ReferenceCard);
throws the exception. What could I have done wrong?
Answer by Bunny83 · May 09, 2011 at 10:19 AM
What's the type of D_ReferenceCard? In your case it have to be of type PlayerCard and have to be attached to a GameObject (can be a prefab). Instantiate just clones the object. It returns the same type that you hand in. If you reference a component (what you have done), the whole containing GameObject gets cloned and the cloned reference of your component is returned.
I can't say much more about that since I have no idea what is returned by getPlayerByID. A final hint: Components (like Monobehaviour) can only exist on GameObjects. It's not possible to create a standalone component.
class MyClass : MonoBehaviour
{
void OnGUI()
{
//...
}
public static MyClass CreateInstance()
{
return (new GameObject).AddComponent<MyClass>();
}
}
the type is PlayerCard aswell. 1.b if that is so, that might be the problem. The problem Ive got with this is, that clone doesnt work in unity. And Id happy to get rid of some of the monbehaviour things, but there are some functions I need to reference actual gameobjects that seem to only work out of a monobehaviour class. (this specially affects GetComponent, which seems to be unusable out if non mono, even though it is used via gameComponent.getComponent (which should be included in unity engine namespace)
getPlayerByID is part of the xml database handler and returns a PlayerCard
http://imageshack.us/photo/my-images/31/unbenanntilc.jpg/
for the better understanding. I know this is against unity directive regarding monobehaviour but till this moment worked perfectly fine. (the parent object is actually attached to a gameobject to provide cascaded OnGUI()
GUIElements are organized in a tree. GUIElement and the classe inheriting from it contain a list of GUIElement thats OnGUI is called onGUI
That's not against Unity's directive ;). Subclasses are a major element of OOP but in Unity you can't create a monobehaviour with "new". You have to use AddComponent on a GameObject. I'll add a sample.
Another way would be to use a class that isn't derived from $$anonymous$$onoBehaviour but provide a public method (interface) that is called from a $$anonymous$$onoBehaviour's OnGUI. I do it that way in my node-based-editor. Greetings from sunny Germany ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613132835im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null variable 2 Answers
iTween NullReferenceException error 1 Answer
Code works in Editor but not in standalone 1 Answer
Instantiate Object with Specific Values 2 Answers