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 JPB18 · Aug 31, 2013 at 05:37 PM · javascriptarraynullreferenceexceptionclassbuiltin

Pushing GameObject into JS Array returns NullReferenceException

Hey there!

I'm trying to obtain a gameObject inside one array of a class, into a JS Array, so that later I can put it inside a builtin GameObject[] array. However when I try to do that, the Editor returns a NullReferenceException. I can assure you that the variable that's trying to be accessed isn't empty.

Here's the class + array in the script shipWeapons.js:

 class WeaponSlot {
     var isEnabled : boolean = false; //checks if the weapon is enabled
     var weapon_go : GameObject; //weapon GameObject. It contains the projectile
     var phaser_point : GameObject; //if the weapon is a beam weapon, it fires from this game object
     var torpedo_point : GameObject; //if the weapon is a torpedo weapon, it fires from this game object
     var pulse_point : GameObject; //if the weapon is a pulse weapon, it fires from this game object
     var nextShot : float = 0.0f; //contains the time reference for when the weapon is able to fire again
     var isAngle : boolean = false; //checks if the target is inside the firing arch
     var isRange : boolean = false; //checks if the target is in range
     var isFiring : boolean = false; //checks if the weapon is firing
     var lastReload : float; //total time it took for the last reload
     
 }
 
 var weapon : WeaponSlot[];

And here's the attempt to build such array:

 //and now the inventory part
     //first get the weapon game objects of each weaponslot
     var Arr : Array;
     
     for(var x : int = 0; x < shipWea.weapon.Length; x++) 
     {
         Arr.Push(shipWea.weapon[x].weapon_go);
     }
     
     var newWeapons : GameObject[] = Arr.ToBuiltin(GameObject) as GameObject[];
     //now place it
     playerShip.shipInv.weapons = newWeapons;

So, does anyone have an idea why this isn't working, and returning a NullReferenceException on Arr.Push()?

Thanks in advance. João Borrego

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
2
Best Answer

Answer by robertbu · Aug 31, 2013 at 06:01 PM

You declare and array variable, but you don't initialize it:

 var Arr : Array = new Array();

But I highly recommend you not use the Array class. Use the .NET collections classes such as List or Stack.

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

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
avatar image JPB18 · Aug 31, 2013 at 06:46 PM 0
Share

Okay, and thanks for the answer ;)

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

16 People are following this question.

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

Related Questions

Static array with custom class? 1 Answer

Populating a Vector2 array in Javascript 1 Answer

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

How to access a variable within a multidimensional array of class 2 Answers

How to increase the size of a class array? 4 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