Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by jihgfee · Sep 06, 2011 at 09:30 AM · destroyarraystypedeclaration

Declaring a type in JavaScript arrays

Hi. I need to use a multidimensional array to store information about certain GameObjects that i instantiate. Problem is that the dynamic JavaScript array turns everything into the type of Object. So when i call the function "Destroy()", i am told that i give it the wrong argument (an object).

//Storing the data in my array

footstepRotation[i] = (180/Mathf.PI)*(Mathf.Atan((currentPosition[i].z-lastPosition[i].z)/(currentPosition[i].x-lastPosition[i].x))); var spawnedFootstep : GameObject = Instantiate(footsteps,currentPosition[i], Quaternion.Euler(0,footstepRotation[i]-90,0)); footstepArray.Add(spawnedFootstep); footstepArrayArray[i] = footstepArray; lastPosition[i] = currentPosition[i];

//function to destroy the footsteps

Destroy(footstepArrayArray[0][0]);

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by SilverTabby · Sep 06, 2011 at 10:15 AM

DO NOT use the JavaScript Dyanimc arrays. I have never gotten them to work the way they are supposed to.

just create a multi-dimensional array like this:

 var footstepArrayArray : GameObject[,] = new GameObject[length1,length2];
 for(var X : int = 0; X < footstepArrayArray.GetLength(0); X++)
     for(var Y : int = 0; Y < footstepArrayArray.GetLength(1); Y++)
         footstepArrayArray[X,Y] = Instantiate(something);
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 jihgfee · Sep 06, 2011 at 10:47 AM 0
Share

A half fix so far - i need to have a dynamic array though - since i do not know how many footsteps i wish to instantiate. - how would you increase the length dynamically with the instantiation of steps?

avatar image
2

Answer by SilverTabby · Sep 06, 2011 at 01:16 PM

If you footsteps do not serve a Gameplay purpose in any way, shape, or form; then you only need a static Array to hold them. Just Destroy() the existing object in the array before putting a new one there. Then when your index >= array.length; index = 0; This way you will be able to keep all your footsteps in an easy to use collection, and it will delete them automatically.

If you need a dynamically sized Collection (the footsteps ARE important to your game), you might want to try a List of GameObjects, or a List of Lists of GameObjects

 import System.Collections.Generic.List;
 var footsteps : List.<GameObject> = new List.<GameObject>();
 
 //you can add bits to the List by hand, or you can
 //   pass an array or another collection into the
 //     constructor to have the contents of your new
 //       list = contents of the parameter.
 footsteps.Add(Instantiate(something));
 
 //you can use a for-each loop to go through a List
 for(var step : GameObject in footsteps)
 {    doSomething(step);    }
 
 //another way to iterate through this collection
 var stepArr : GameObject[] = footsteps.ToArray();
 for(var count : int = 0; count < stepArr.length; count++)
      doSomething( stepArr[count] );

 var listOfLists : List.<List.<GameObject>> = new List.<List.<GameObject>>();
 var list : List.<GameObject> = new List.<GameObject>();
 listOfLists.Add(list);
 
 for(var l : List.<GameObject> in listOfLists)
     for(var stp : GameObject in l)
          doSomething(stp);


Comment
Add comment · Show 2 · 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 spinaljack · Dec 23, 2012 at 11:19 AM 0
Share

I've tried this code in Unity and it wont compile: var listOfLists : List.> = new List.>();

It keeps showing expecting > but found >>

Deleting the second > brings up a new error expecting > but found ;

Can someone tell me what is the correct way to declare a list of lists?

avatar image spinaljack · Dec 23, 2012 at 01:55 PM 0
Share

Ok found the answer, leave a space between then two end > >

avatar image
0

Answer by aldonaletto · Sep 06, 2011 at 12:27 PM

As far as I know, the only way to typecast a value in Unityscript is to store the value in a variable of the correct type, like this:

var go: GameObject = footstepArrayArray[0][0];
Destroy(go);

It may solve the typecast problem, but I really don't know how you will sweep the arrays of arrays to delete each element - hope you know what you're doing!

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 jihgfee · Sep 06, 2011 at 12:30 PM 0
Share

I tried this kind of type casting though it didn't work.. I got the problem cleared by sorting the GameObjects by name ins$$anonymous$$d - a sneaky little fix - but atleast it works for now (:

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

Remove dead enemies from array 1 Answer

Using DestroyImmediate on Arrays? 1 Answer

How to destroy a unit contained in multiple lists 1 Answer

Variable resetting back to zero 2 Answers

Arrays declaration c# 3 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