• 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 hzigy · Jun 28, 2011 at 07:00 PM · errorinspectorclasstypepublic

Public class used in another class (C#)

Ok, i have a sort-of advanced problem, which i'm sure has a simple solution, but i just can't manage to find it...

Let's say i have a public class, designed to be editable from the inspector...

 [System.Serializable]
 public class PersonType {
     string gender;
     bool schoolEmployee;
     GameObject model;    
 }

...and i use this class in another class, which i assign to a gameobject, where i set several instances of it (everything filled in inspector)...

 public class PeopleTypes : MonoBehaviour {    
     public PersonData maleStudent;
     public PersonData femaleStudent;
     public PersonData Teacher;
     public PersonData Janitor;
 }

...and i use the PersonType class as a part of another class (Person type defining the type of a person, Individual defining the individual parameters)...

 public class Individual : MonoBehaviour {
     string name;
     int age;
     PersonType pType;    
 }

...why do i get a nullreference exception when i try to set...

 Individual ind_1 = new Individual();
 ind_1.pType = PeopleTypes.maleStudent; //<- this is where i get the error

...or better yet, what would be the correct way of setting this? I guess a part of the problem would be whether this is the correct way to set up a public class for inspector editing...

Comment
Add comment · Show 4
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 Dreamblur · Jun 28, 2011 at 07:43 PM 0
Share

You can turn those into structs if all they're gonna be are storage/placeholders.

avatar image burnumd · Jun 28, 2011 at 07:59 PM 0
Share

I think custom structs aren't serialized in the Inspector.

avatar image BigBulle · Jun 28, 2011 at 08:11 PM 0
Share

I don't understand your code... What is PersonData? Is it PersonType? Why do you access to maleStudent like a static member of PeopleTypes? From where do you call the GetComponent?

avatar image hzigy · Jun 28, 2011 at 08:32 PM 0
Share

Yeah, sry, PersonData should be PersonType. I was simplyfying an example. Anyway, the problem is solved. Tnx for help :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GuyTidhar · Jun 28, 2011 at 07:18 PM

You must not use new to instantiate MonoBehaviour classes. Instead, you should instantiate components or game objects or don't inherit from MonoBehaviour when you want to use "new".

If you don't need your class to have its own update, coroutine and the general Unity processing functionality, just use the classes you wrote without inheriting (or inheriting from non mono base class).

http://unity3d.com/support/documentation/ScriptReference/index.Instantiate.html

Comment
Add comment · Show 3 · 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 hzigy · Jun 28, 2011 at 07:55 PM 0
Share

Having removed the $$anonymous$$onoBehaviour from the class 'Individual' there is 1 less warning, but no change in functionality - the error is still there.

It might be of some value to note, that peopleType from peopleType.malestudent in the example above is received by 'PeopleTypes peopleType = GetComponent("PeopleTypes") as PeopleTypes;

Tnx for the help so far :)

avatar image GuyTidhar · Jun 28, 2011 at 08:07 PM 0
Share

When and where are you calling GetComponent?

avatar image hzigy · Jun 28, 2011 at 08:30 PM 0
Share

It's called in the Awake routine. It is then used in the same routine further down. Both scripts (PeopleTypes and the script that calls it) are on the same game object....wait a second...GAH! There was a typo in the GetComponent parameter string....boy is my face red

Well it works now (i knew it was something dumb), i was taking the GetComponent part for granted and failed to double check :P

Tnx for everyones help!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inspector not showing my public changes. 1 Answer

Weird error while trying to call a void from another class 1 Answer

Default Serialization Values Not Being Set 0 Answers

Accessible collection of Type 1 Answer

How do I create a JavaScript class that creates an object with viewable variables in the inspector? 3 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