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 TheAssassin · Nov 02, 2011 at 09:34 AM · javascriptarrayclassstatic

Static array with custom class?

So I thought I had figured out static variables until I ran into this problem:

Works:

 //ScriptOne
 static var lot = new int[4];
 static function output () {
     var ret = lot[0];
     return ret;
 }

Doesn't work:

 //ScriptOne
 class pig{
     var weight:int=6;
 }
 
 static var lot = new pig[4];
 static function output () {
     var ret = lot[0].weight;
     return ret;
 }
 
 //ScriptTwo
 function Update () {
 Debug.Log(ScriptOne.output());
 }

NullReferenceEception on play

It appears that an array whose type is a custom class cannot be made static. Is there a workaround or a better way to do this that works? I am writing a script that I want to be treated as a library of functions and in this case there should only be one instance of the array at a time so it made sense to make it static. Am I missing something?

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

Answer by gfr · Nov 02, 2011 at 09:57 AM

You have initialized the array, but not it's contents. For basic types (like ints) that fine, but not for class-types. You need to initialize the array contents once.

Comment
Add comment · Show 4 · 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 TheAssassin · Nov 02, 2011 at 08:10 PM 0
Share

Yeah I initialized it. var lot = new lot[] is the same as var lot:lot[] = new lot[4];

I also tried changing a variable using an initialize function that I called in the start of scriptTwo and the results were the same. If you are sure that's whats going on could you post an example?

avatar image gfr · Nov 02, 2011 at 09:27 PM 0
Share

@TheAssassin: I meant actually initializing the contents of the array. For class-types those are references and have to be set to specific instances, otherwise they refence nothing. $$anonymous$$g.:

 for (var i=0; i<lot.length; ++i)
     lot[i] = new pig();

This is the same with non-static arrays.

avatar image TheAssassin · Nov 03, 2011 at 12:41 AM 0
Share

Sorry I was in a hurry and didn't fully look into what you were saying. It works Great thanks!

avatar image gfr · Nov 03, 2011 at 01:16 AM 0
Share

No problem, i guess putting in a code-snippet right away would have been better to avoid misunderstandings.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with sorting values from a class 2 Answers

Change a Class in the inspector 1 Answer

How to increase the size of a class array? 4 Answers

Reading and Storing External Data into Memory (From Text) 1 Answer

Array of a class deleting previous array element 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