Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by T3PeS · May 09, 2011 at 09:55 AM · instantiatenullreferenceexception

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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>();
    }
}
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image T3PeS · May 09, 2011 at 10:54 AM 0
Share
  1. 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)

  2. getPlayerByID is part of the xml database handler and returns a PlayerCard

avatar image T3PeS · May 09, 2011 at 11:03 AM 0
Share

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

avatar image T3PeS · May 09, 2011 at 11:05 AM 0
Share

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

avatar image Bunny83 · May 09, 2011 at 11:47 AM 0
Share

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.

avatar image Bunny83 · May 09, 2011 at 12:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Null variable 2 Answers

NullReference on trying to instantiate a prefab which has been set using scriptable object,Particle giving null reference but works in scene view 0 Answers

iTween NullReferenceException error 1 Answer

Code works in Editor but not in standalone 1 Answer

Instantiate Object with Specific Values 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges