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
2
Question by AlucardJay · Mar 26, 2012 at 02:28 PM · arrayclear

correct method for clearing a gameObject array

Hi. What is the correct accepted method of clearing an array of gameObjects ?

e.g.

 var arrayPathMarker : GameObject[] = new GameObject[100];
 
 for (var i = 0; i< 100; i++) {
     Destroy (arrayPathMarker[i]); // this , or the next command ?
     // arrayPathMarker[i]=null;
 }
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

4 Replies

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

Answer by Eric5h5 · Mar 26, 2012 at 02:37 PM

Do you want to just clear the array, or destroy the objects? Setting the entries in the array to null will clear the array, but won't destroy the objects. (Although you can just use System.Array.Clear.) Using Destroy will remove the objects, and the entries will be null. GameObjects are by reference.

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 AlucardJay · Mar 26, 2012 at 03:03 PM 0
Share

I did want to Destroy the objects, and now knowing I can set to null without destroying the object is a bonus. Thankyou.

avatar image MountDoomTeam · Oct 29, 2012 at 12:10 AM 0
Share

I just did this System.Array.Clear.(foundwalls);

And it said system.array.clear is not a generic definition-it's in JavaScript, how should I write it?

avatar image Eric5h5 · Oct 29, 2012 at 12:17 AM 0
Share

Function names can't have a "." after them. System.Array.Clear (foundwalls)

avatar image MountDoomTeam · Oct 29, 2012 at 04:55 AM 0
Share

thanks, it still said
The best overload for the method 'System.Array.Clear(System.Array, int, int)' is not compatible with the argument list '(UnityEngine.GameObject[])'.

this one runs, although in JavaScript it seems to have an error, it prints the same array.length

             System.Array.Clear(foundwalls,0,foundwalls.length);

              print( "clr"+foundwalls.length);//no difference

this also

        foundwalls = new GameObject[0];

         print( "clr"+foundwalls.length);

-sorry I was getting an error because I was forgetting to clear the array counting variable as well as the array.

avatar image Eric5h5 · Oct 29, 2012 at 06:09 AM 0
Share

Right, you need to specify the index and the length. It's not supposed to change the length of the array; it just clears the contents. Please read the docs...arrays have a fixed size. If you want to change the size, use List ins$$anonymous$$d of an array.

avatar image
0

Answer by Mastasytha · Feb 23, 2014 at 12:01 PM

I have found a method that works fine with me it checking to see if the game object is null here is my spawn script were i have implemented my code were i destroy the spawn point after it is used

public var Respawn : GameObject[];

function Start () { Respawn = GameObject.FindGameObjectsWithTag("Respawn");

}

function Update () { } function rs() { var spawn = Random.Range(0,Respawn.Length); if(Respawn[spawn] == null) { rs(); }else{ transform.position = Respawn[spawn].gameObject.transform.position; Debug.Log(spawn); transform.position.y = 2; Destroy(Respawn[spawn].gameObject); }

}

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
avatar image
0
Wiki

Answer by Mastasytha · Feb 23, 2014 at 01:53 PM

this is the code i used in my script to fix my errors with null game objects in an array

pragma strict

 public var Respawn : GameObject[];
 var spawnpoint : GUIText;
 function Start () {
 Respawn = GameObject.FindGameObjectsWithTag("Respawn");
     
 }
 
 function Update () {
 }
 function rs()
 {
     var spawn = Random.Range(0,Respawn.Length);
     if(Respawn[spawn] == null)
     {
     rs();
     }else{
     transform.position = Respawn[spawn].gameObject.transform.position;
     Debug.Log(spawn);
     transform.position.y = 2;
     Destroy(Respawn[spawn].gameObject);
     }
     

}

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

Answer by skylem · Jan 10, 2015 at 08:58 AM

There is one alternative to this i thought im not sure how useful it will be to others but has helped me plenty so here it is, You can create a list based from your array, using a foreach to add each gameobject to your list, i feel lists are easier to use and clear especially so for anyone who is new to scripting, if anyone would like a step by step for this just email me Skylem@live.com.au and i'll provide a blow by blow of how to use a list aswell as an example.

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

7 People are following this question.

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

Related Questions

Problem when trying to save variable in another variable. 1 Answer

Clear Entire Array, Set To Null(C#) 2 Answers

help with memory leak 1 Answer

Put objects in a row and give them specific positions 1 Answer

How do you completely clear a mesh? 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