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 Somian · Jan 20, 2012 at 07:37 PM · javascriptarraysloops

create an array of unknown size in a loop

Hi there, I have a loop that fills an array. basically I go through a simple puzzle game level, look for vacant locations and write the location in the list. Later, I pick a random location from the list and do something.

 var availableLocations=new Block[xwidth*ywidth];
 
         var internalLoop:int=0;
         //x axis loop
         for(x=1;x<ywidth-1;x++)
         {
             //y axis loop
             for(y=1;y<ywidth-1;y++)
             {
                 if(levelArray[x,y].type==BlockType.Nothing) //check if there is nothing yet at the location
                 {
                     availableLocations[internalLoop]=levelArray[x,y]; //add the location to the list of available locations
                     internalLoop+=1;
                 }
             }
         }
 
 //create a goal block at one of the available locations
         var r=Mathf.Floor(Random.value*availableLocations.length);
         print("trying to create a gloal block at location"+r);
         var goalBlock=availableLocations[r];
         goalBlock.type=BlockType.Goal;
         goalBlock.color=levelColors[currentGoalBlockColor];

the problem is, that my array is too one and I sometimes get nullReferenceExceptions because I tried to access an object that wasn't written in my array because the array is as big as the whole level, but the amount of vacant locations is only a part of the level.

note: at the last line I use a method of the Block at the picked location in the array. I was thinking about using the native javascript array, but the problem is that, if I do something like Array[].method with it, I get an error because the items are generic objects instead of the classed i've used so I can't use methods (at least as far as I now I can't as I tried)

How can I make availableLocations[] be only as big as internalLoop gets during the loop?

thanks in advance!

Comment
Add comment · Show 1
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 Fattie · Jan 20, 2012 at 10:52 PM 0
Share

Simply use a "Javascript Array" (read up on them) ... but moreover, don't use those!

They are out of date. Simply use the LIst form. ( import System.Collections.Generic; private var rr : List.; etc etc )

See here for a massive incidental discussion:

http://answers.unity3d.com/questions/198318/javascript-array-use-with-a-struct-.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jan 20, 2012 at 08:09 PM

Have you tried ArrayLists ? I'm not sure of the name in javascript, but it's probably what you need.

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

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

6 People are following this question.

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

Related Questions

how to save an array into an object 1 Answer

Javascript Multidimensional Arrays 0 Answers

JesseEtzler's RPG Talent System in C# 1 Answer

My array does not update when object is destroyed. How do I fix it? (java) 2 Answers

How to populate an array from an external text file 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