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
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
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 MonoBehaviour but provide a public method (interface) that is called from a MonoBehaviour's OnGUI. I do it that way in my node-based-editor. Greetings from sunny Germany ;)
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
No one has followed this question yet.
instantiate prefab object to existing object
Null Reference when trying to instantiate a prefab not in the scene
Null reference on instantiated object
NullReferenceException: Object reference not set to an instance of an object
NullReferenceException with MoveObject.js