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 /
This question was closed Nov 09, 2016 at 05:13 PM by OussamaB for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by OussamaB · Sep 03, 2013 at 03:33 AM · javascriptarraynullreferenceexceptionsize

NullReferenceException weird error

I have a class array that I set its size from the "Awake" function and then use in different functions. The problem is that when I start unity and click on play or build my game to test it, I start getting this error:

 "NullReferenceException: Object reference not set to an instance of an object"

Coming from this line:

 Slot[i].IsTaken = false;

Which is the first line where I start using my array. I thought I had a problem with my array size so I debugged the array size in the "Update" function and I got the same size that I set in the "Awake" function. The weird thing is that when I click on the game object that holds this script, the errors are gone and it starts working fine. But this is not a solution since there's no way it will work outside the unity editor with this error. Oh, and by the way, I'm using Javascript. Is this a common problem with arrays in unity? How can I fix it?

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

  • Sort: 
avatar image
0
Best Answer

Answer by _Shockwave · Sep 03, 2013 at 08:19 AM

Provide more information around IsTaken and how you created the array but from what I can guess, it's likely that Slot[i] is returning null.

Setting the size of an array does not initialize it with a value, it only register X amount of space for it in memory. Confirm that Slot[i] is not null and that you are initializing it with a value correctly before calling this line:

 Slot[i].IsTaken = false;
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 Jamora · Sep 03, 2013 at 08:36 AM 0
Share

Actually, declaring an array does set each index with a default value for the given type. The default values for value types can be found here, and null is the default for all reference types.

Because your question gives a reason why you do not excpect a null reference error and Shockwave answered it (correctly, I'd say), I'll just give you a link to my guide to what a null reference error is and how you can fix it yourself. Read it here.

avatar image _Shockwave · Sep 03, 2013 at 08:49 AM 0
Share

"Actually, declaring an array does set each index with a default value for the given type. The default values for value types can be found here, and null is the default for all reference types." -- Yes, you're correct. I thought they only occupied space and set to null.

avatar image OussamaB · Sep 03, 2013 at 02:30 PM 0
Share

I get what you are saying guys. I think can confirm that Slot[i] is not null since I have debugged the size of the Slot array and "i" is a value co$$anonymous$$g from this loop:

 for(var i : int = 0; i < Slot.Length; i++)

So Slot[i] can't be null, am I right? and this how I created my array:

class SlotVars { var Name : String; var IsTaken : boolean = false; } var Slot : SlotVars[];

But again, as I stated in my question the weird thing is that when I select on the game object that holds this script, the error is suddenly gone.

avatar image Jamora · Sep 03, 2013 at 03:30 PM 0
Share

Unless you have a Slot[i] = new yourClassNameHere(); somewhere before accessing Slot[i], the initialized value will be null. You can debug if any element is null by Debug.Log(Slot[i]);. If that says something other than null and you get the error, this is indeed strange.

avatar image OussamaB · Sep 03, 2013 at 04:20 PM 0
Share

Thanks a lot Jamora. Please check Jamora's comment for the correct answer.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image

Related Questions

NullReferenceException with Javascript Array 2 Answers

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

Populating a Vector2 array in Javascript 1 Answer

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

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