x


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?

more ▼

asked May 09, 2011 at 09:55 AM

T3PeS gravatar image

T3PeS
1 9 6 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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>();
    }
}
more ▼

answered May 09, 2011 at 10:19 AM

Bunny83 gravatar image

Bunny83
75.8k 25 91 327

1.a 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)

  1. getPlayerByID is part of the xml database handler and returns a PlayerCard
May 09, 2011 at 10:54 AM T3PeS

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()

May 09, 2011 at 11:03 AM T3PeS

GUIElements are organized in a tree. GUIElement and the classe inheriting from it contain a list of GUIElement thats OnGUI is called onGUI

May 09, 2011 at 11:05 AM T3PeS

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.

May 09, 2011 at 11:47 AM Bunny83

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 ;)

May 09, 2011 at 12:04 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2852
x766

asked: May 09, 2011 at 09:55 AM

Seen: 1411 times

Last Updated: May 09, 2011 at 09:55 AM