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 vik.vega · Jun 22, 2013 at 06:02 PM · staticlists

Generic List in static function

Hi, I'm working with static functions, and in this sample code there is my test class.

 // SimpleClass.js
 #pragma strict
 import System.Collections.Generic;
 
 static var randomnumbers:List.<int>;
 static var randomnumber:int;
 
     static function RandomOne()
     {        
     randomnumber = Random.Range(0,11);
     return randomnumber;
     }
     
     static function RandomList(howmany:int)
     {
         for (var i = 0;i<howmany;i++)
         {
         var tempvalue:int = Random.Range(0,11);
         randomnumbers.Add(tempvalue);
         }
     return randomnumbers;
     }

Now, when I call the first method, the one returning a random int, no problems

 number = SimpleClass.RandomOne();

the problems shows up when I try to call the second method, the one returning a populated list of random ints

 somerandomnumbers = SimpleClass.RandomList(5);

it yields "NullReferenceException: Object reference not set to an instance of an object" upon trying to add an element to the static array. Don't get me wrong, I don't really need that array to be static, it's just that as far as i can see, static methods, can only deal with static variables. And what i'm trying to achieve, is pretty much having a collection of static functions without being carried by an actual gameobject. (so delightfully calling them MyTools.someTools(something,something);

thank you ! :)

Comment
Add comment · Show 4
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 KiraSensei · Jun 22, 2013 at 06:13 PM 0
Share

I think there are some code missing ... And a question :)

avatar image vik.vega · Jun 22, 2013 at 06:25 PM 0
Share

actually none of them should be missing :) i try to carefully read before posting. anyway, thanks for the attention, and here's again the question:

Is there any limitation/special case when working with Lists, when you make them static, [to use them in a static function (to be able to call it likewise in the two lines you refer to as missing code)]

thank you again.

avatar image vik.vega · Jun 22, 2013 at 06:27 PM 0
Share

Oh but of course you probably refer to my previous attempt at editing, gone horribly wrong :)

avatar image KiraSensei · Jun 22, 2013 at 06:31 PM 0
Share

Yes, I posted my comment in between :)

1 Reply

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

Answer by Eric5h5 · Jun 22, 2013 at 06:27 PM

You haven't initialized the randomnumbers list anywhere, hence the null reference error. If you want to return a unique list every time the RandomList function is called, however, the randomnumbers variable should not be a global static variable, instead it should be a local variable declared inside the RandomList function. Same goes for the RandomOne function; there's no reason for it to return anything if you're using a global static variable.

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 vik.vega · Jun 22, 2013 at 06:36 PM 0
Share

Here's what I was waiting for :) Thanks Eric, now it's clear.

But what would you suggest, since inside my implementation of a pathfinding script, I need a global(for every pathfinding request) List inside of my pathfinding method as a [queue for recursive calling][1]

Thank you ! [1]: http://en.wikipedia.org/wiki/Pathfinding#Sample_algorithm

avatar image Eric5h5 · Jun 22, 2013 at 06:58 PM 0
Share

In that case it's O$$anonymous$$ to leave things as they are, though for the sake of correctness, the variables should be made private, since you're only accessing them through the functions.

avatar image vik.vega · Jun 22, 2013 at 07:41 PM 0
Share

Thanks !

i'm inizializing randomnumbers this way

 static private var randomnumbers:List.<int>;

as a global variable inside my class, right after randomnumber:int; and it still still says "Object reference not set to an instance of an object" am I initializing it wrong ?

avatar image vik.vega · Jun 22, 2013 at 07:44 PM 0
Share

never$$anonymous$$d, i wasn't instancing it properly, will keep updated my progress in understanding the original topic of the question :)

avatar image Eric5h5 · Jun 22, 2013 at 07:50 PM 0
Share

Right, that's just declaring the variable, not initializing it.

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

17 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

Related Questions

Creating a list using a list from another script? 1 Answer

Trouble Using a Static List 0 Answers

How to make a list store and save values even while changing scenes. 1 Answer

How does one inspect static vars? 3 Answers

Problem with Static Variable? 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