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
0
Question by Mathias · Sep 10, 2011 at 11:46 AM · gameobjectarrayphysics.overlapspherebce0022

Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere

Hi

I am trying to make a more versatile way of entering vehicles I kinda am trying to "soft code" it instead of "hard coding" it like I did in the past.

 var radius = 3.0;
 function FindClosestVehicle(){
     
     var gameObjects : GameObject[] = Physics.OverlapSphere (transform.position,radius); // Error here
     
     for (var hit : GameObject in gameObjects) {
         if (!hit)
             continue;
         
         if (hit.gameObject){
             print(hit.gameObject.name);
            
     }
 }
 }

But is says "Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]'" At the third line of code. I cannot see any collider information there unless OverlapSphere is only for collecting colliders. If so how would one go about finding gameObject using Physics.OverlapSphere?

Thank you my friend.

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 SisterKy · Sep 10, 2011 at 12:28 PM 0
Share

Please always use error-codes as tags, thank you =)

3 Replies

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

Answer by Bunny83 · Sep 10, 2011 at 12:17 PM

The physics system only works with colliders so [OverlapSphere][1] returns an array of colliders which bounds touches the sphere. An array of Colliders can't be "casted" or "converted" into a GameObject array. You have to do it manually.

Colliders are components so they are always attached to a GameObject. To access this GameObject you can just use .gameObject on a collider.

Btw. in your for loop you have useless redundancy. The hit variable is of type GameObject so hit.gameObject is the same as hit.

However OverlapSphere doesn't return a GameObject array so you should change your code like this:

 var colliders : Collider[] = Physics.OverlapSphere (transform.position,radius);
 
 for (var current : Collider in colliders)
 {
     print(current.name);
     // To access the GameObject of that collider use:
     // current.gameObject
 }


Oh and all Components attached to a GameObject share the gameobjects name, tag and layer so you don't need to use current.gameObject.name [1]: http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html

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 SisterKy · Sep 10, 2011 at 12:25 PM

it's all but elegant, but I think you could do something along the line of

var colliders : Collider[] = Physics.OverlapSphere (transform.position,radius);
var gameObjects : GameObject[];
for (var col : Collider in colliders)
{
  gameObjects.Add(col.gameObject);
}

[untested code!]
It should work, but there has to be a nicer way...?

Edit: Aw, Bunny was faster =3 ... so there's no other way after all o.o

Greetz, Ky.

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 Peter G · Sep 10, 2011 at 01:38 PM

A nice looking approach is to convert the whole array using a converter.

 var radius = 3.0;
 function FindClosestVehicle(){
 
     var colliders = Physics.OverlapSphere (transform.position,radius);
     var gameObjects = System.Array.ConvertAll.<Collider , GameObject>( colliders , function(x) x.gameObject );
 
     for (var hit : GameObject in gameObjects) {
         if (!hit)
             continue;
 
         if (hit.gameObject){
             print(hit.gameObject.name);
 
     }
 }
 }

The nice thing about that is you don't have to rewrite your enumerator since you will have a gameObject array. there will be no need to change the enumerator to change the collider to a game object in the loop.

Note: I don't know if you can use generic methods in js. I know generic types work, but I've never tried generic methods so I'm writing it in C# too

 float radius = 3.0f;
 public void FindClosestVehicle(){
 
     var colliders = Physics.OverlapSphere (transform.position,radius);
     var gameObjects = System.Array.ConvertAll<Collider , GameObject>( colliders , x => x.gameObject );
 
     for (var hit  in gameObjects) {
         if (hit == null)
             continue;
 
         else {
             print(hit.name);
 
     }
 }
 }
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert String to GameObject[] type Array 2 Answers

Transport unknown amout of objects with GameObject array 0 Answers

View an array of the transform position/rotation of all game objects with a specified tag., 0 Answers

Access a String array in one script from another script 0 Answers

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 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