• 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 efreet · Jun 27, 2011 at 03:45 AM · javascriptnullreferenceexceptionclass

varible type class not working if not declared public

This code doesn't work unless var testing : Test is declared outside of Start(). It returns a NullReferenceException: Object reference not set to an instance of an object. Sorry about the bad title.

Thanks for any insight.

class Test { var a : boolean; var b : int; }

function Start() { var testing : Test; testing.a = false; }

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

2 Replies

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

Answer by Flynn · Jun 27, 2011 at 03:49 AM

var testing : Test = new Test();

This goes into memory handling in code -- There are two types of variables, basic variables, and objects.

Basic variables are ALL either a number type (float, integer, double, long, short, word, long long, etc) or a boolean value. They are stored in a special part of memory.

Then, you have objects. All object variables are actually (internally) numbers referring to a point in memory that contains the actual object. The quirk of this is that objects must be created with "new Type()", allocating a spot in memory, and telling the variable where to point to. Because basic values are smaller, and less dynamic than objects, they don't need a reference to their location. Instead of a reference, they just have the value they store, so, they get defined automatically.

In short, any custom class must be created with new ClassName()

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 efreet · Jun 27, 2011 at 03:52 AM 0
Share

thanks. But why doesn't it always need to be declared like that?

avatar image Flynn · Jun 27, 2011 at 03:53 AM 0
Share

Hi, I was actually typing it up as you accepted :)

avatar image efreet · Jun 27, 2011 at 03:57 AM 0
Share

makes sense, thanks again

avatar image Bunny83 · Jun 27, 2011 at 04:29 AM 0
Share

This is a Unity "feature". All public variables are initialized by Unity when they belong to a $$anonymous$$onoBehaviour so they can be edited in the inspector.

avatar image Flynn · Jun 27, 2011 at 04:31 AM 0
Share

Unity does definitely define object type variables for you if they are public. Auto definition of simple/basic variable types though is something existent throughout all program$$anonymous$$g

avatar image
1

Answer by Eric5h5 · Jun 27, 2011 at 04:08 AM

If you make it a struct, then it has default values of 0 like Vector2, Vector3, etc., and you don't need to use new ClassName(). Small simple classes that only use base types like int, float, etc. are good candidates for structs.

 class Test extends System.ValueType
 {
  var a : boolean;
  var b : int;
 }
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Is there a way to make a class destructor in javascript? 2 Answers

Split() into a fixed length array? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 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