Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Subject-Gabz · Jan 21, 2015 at 04:35 PM · c#arraycollider 2d

Can't create an Collider2D array?

I have been trying to create an array of collider2D so i can disable all the respective colliders of an children's object.

But this error pops-up and i have tried to figured out what's wrong but nothing seems wrong to me, or I'm not seeing it.

ERROR: Cannot implicitly convert type UnityEngine.Collider2D' to UnityEngine.Collider2D[]'

What I'm I doing wrong?

 private Collider2D[] childrenColliders;
 private bool destroied= false;
 
 void Update (){
 
     if (assetHitPoints<=0 && destroied == false){
             Destroy();
     }
 }
 
 void Destroy (){
 
     Debug.Log("Destroying");
             
     childrenColliders = GetComponentInChildren<Collider2D>();
 
     foreach (Collider2D colliders in childrenColliders){
         colliders.enabled = false;
     }
 
     particleSystem.Play();
     destroied=true;
 
 }
 


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

1 Reply

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

Answer by Bentley · Jan 21, 2015 at 06:21 PM

You are getting that error because GetComponentInChildren() is returning a single Collider2D, and what you want is an array of Collider2D's, or Collider2D[]. What you'll want to do is something like this:

 List<Collider2D> childrenColliders = new List<Collider2D>();
 GetComponentsInChildren<Collider2D>(childrenColliders);

The first line replaces line 1 of your code and the second replaces line 15. You can then iterate through the list instead.

Also as a side note: if you are working with an unknown number of items you will probably want to use a List since Lists can grow and shrink as needed using the .Add() method, while arrays must be initialized with a set capacity.

Hope this helps,

Bentley

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 Subject-Gabz · Jan 21, 2015 at 06:35 PM 0
Share

Thanks Bentley, it helped me solve my problem, and from now on I'll take your side note in consideration before using Arrays or a List.

Appreciated.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Set Current Array GameObject to Active 1 Answer

How to check for an empty array? 1 Answer

Deleting an Element from an Array. 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