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 Seth-McCumber · Oct 15, 2013 at 09:15 PM · arrayclear

Clear Entire Array, Set To Null(C#)

I Need Some Help With Clearing An Array Of GameObjects, Not Destroying The Objects, Just Removing Them From The Array. Another Way To Put This Is Set The Array To Null. I Have Looked At The Unity Scripting Reference, And It Says (NameOfArray).Clear(); Should Work, But I Tried RandDoor.Clear(); , And I Get The Compiler Error

Assets/Scripts(Mine)/Game/Moving Around The Castle/EnterCastleDoor.cs(34,26): error CS1501: No overload for method Clear' takes 0' arguments

Help Anyone? Thanks!

-Seth

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

2 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by LividRabbit · Oct 15, 2013 at 09:52 PM

Array.clear has 3 parameters, Array array, int index , int length, if you want to clear the entire array, try Array.Clear(RandDoor, 0, RandDoor.Length);

Comment
Add comment · Show 11 · 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 Seth-McCumber · Oct 16, 2013 at 10:29 AM 0
Share

Gives $$anonymous$$e Yet Another Error,

 Assets/Scripts($$anonymous$$ine)/Game/$$anonymous$$oving Around The Castle/EnterCastleDoor.cs(35,17): error CS0103: The name `Array' does not exist in the current context
avatar image LividRabbit · Oct 16, 2013 at 07:43 PM 1
Share

did you put using System; at the top of your code?

avatar image Shippety LividRabbit · Oct 05, 2017 at 07:15 PM 0
Share

Thanks, worked for me!

avatar image Seth-McCumber · Oct 16, 2013 at 10:14 PM 0
Share

No, Will Try Soon

avatar image BeB_Wir3 · Oct 19, 2013 at 12:00 AM 0
Share

i have the same problem, and tried your method with my array name: Array.Clear(RandomImages, 0, RandomImages.Length); now "using System.Collections;" is at the 2nd line of my script, but still the error "Assets/Scripts/GameController.cs(79,13): error CS0103: The name `Array' does not exist in the current context" is displayed. what do?

avatar image Jamora · Oct 19, 2013 at 02:49 AM 0
Share

The Array class is in the System namespace. The correct using directive is "using System;". Alternatively, you can just type "System.Array.Clear(RandDoor, 0, RandDoor.Length);"

Show more comments
avatar image
1

Answer by vfxjex · Jul 01, 2015 at 04:54 AM

Why not just type

 int[] a = new int[]{1,2,3,4};
 //to clear
 a = new int[4];
 
 

a will now equals to null; you don't even need to use System;

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 losingisfun · Jan 28, 2017 at 04:34 AM 0
Share

Haha! this is the best! I think we made a classic example of overcomplicating a rather simple issue

avatar image Bunny83 losingisfun · Jan 28, 2017 at 05:53 AM 1
Share

Well, if you still need the array this is actually not the best solution. It creates a new array and the old one will eventually get garbage collected. Using Array.Clear just sets the elements to null / default values. So using Array.Clear no new memory is allocated.

Btw: @Hoeloe already suggested this solution in a comment above about two years earlier.

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

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

Related Questions

correct method for clearing a gameObject array 4 Answers

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

help with memory leak 1 Answer

How to Make 2 dimension array of classes 1 Answer

Array index is out of range 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