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 customphase · Mar 14, 2012 at 08:40 PM · nullreferenceexceptionclasses

Custom class and null reference exception

Hello there. I have a problem with a custom created class. This is the tEnemy.js script.

 class tEnemy
 {
  var gameobj:GameObject;
  var hp: int;
  function create()
  {
  gameobj = GameObject.CreatePrimitive(PrimitiveType.Cube);
  }
 }

Now when in the main script i try to create some tEnemies:

    var ob: tEnemy;
    ob.create();
    ob.gameobj.AddComponent(Rigidbody);
    ob.gameobj.transform.rotation.x = Random.value;
    ob.gameobj.transform.rotation.y = Random.value;
    ob.gameobj.transform.rotation.z = Random.value;
    ob.gameobj.transform.localScale = Vector3 (enemySize, enemySize, enemySize);
    ob.gameobj.transform.position.x = -2+Random.value*6;
    ob.gameobj.transform.position.z = -3.4+Random.value*6;
    ob.gameobj.transform.position.y = 2;   
    ob.hp = 4;

I get a NullReferenceException and nothing happens. Please help me, what am i doing 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

2 Replies

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

Answer by syclamoth · Apr 16, 2012 at 05:41 AM

This happens because the line

 var ob : tEnemy;

Only creates a 'tEnemy' shaped hole, that can have a tEnemy object put in it at a later state- it doesn't create the object itself! The expression

 new tEnemy();

creates an object, but without a slot to put it in it's not much use to you. This is why the two lines are usually written together, like so:

 var ob : tEnemy = new tEnemy();

This crates the slot, and simultaneously fills it with an object.

I see that you are using an initialisation function, 'create' in your custom class. Consider using the constructor for this, so that you only need to use the 'new' statement, without calling 'create' seperately!

 // Put this in the tEnemy class, and it will be called
 // every time you create one!
 function tEnemy()
 {
     create();
 }
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
avatar image
0

Answer by aviose · Apr 16, 2012 at 05:33 AM

I had a lot of issues with a similar, but more deeply embedded version of this error. If you want to actually alter the member properties of the class, then simply telling the variable it is of type:class doesn't work. You HAVE TO establish the object with an explicit declaration... To draw it out, your first line on your enemy creation should read:

 var ob: tEnemy = new tEnemy();

If you don't use that format, with the var fu : class = new class(); format, it will give you all sorts of null errors.

(When I got the error it was because I had stats that broke down in to smaller versions, like current/max or base/modValue. I declared the base object properly but not the modValues/base aspect of the stats. Since Unity Editor doesn't tell you where to really look for these errors it took me FOREVER to find the solution.)

Comment
Add comment · Show 1 · 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.

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

Array of custom properties? (C#) 1 Answer

NullRerenceException On comparisons 0 Answers

should class members ever be static? 1 Answer

How to add an object to a MonoBehaviour in JavaScript? 1 Answer

Instantiate inside class function returns NullReference 1 Answer


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