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
0
Question by sgmongo · Oct 15, 2011 at 10:28 PM · colliderarrayoverlapsphere

OverlapSphere returned collider arrays

Back again with new problems :D

I have some code attached to an object in my gameworld. I've litterally just started building my own AI for the first time and I'm finding a gap that needs filling in my knowledge. First off, the scene is simple. One cube, on layer 12 positioned close to a second object. Second object has the following code attached as a JS.

var heardist : float = 50f; var zmask : int = 1 << 12; var stuff : Vector3;

function Update () {

 var colliders : Collider[] = Physics.OverlapSphere (transform.Position, heardist, zmask);
 
 for (var hit in colliders) {
     if (!hit)
         continue;
     
     if (hit.collider) {
         Debug.Log(hit.collider);
         stuff = hit.collider.transform.position;
         Debug.DrawLine(transform.position, stuff, Color.red);
         
         
         //hit.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
     }
 }

}

I get the following errorcode:

Object refrence not set to and instance of an object

It highlights this line var colliders : Collider[] = Physics.OverlapSphere (transform.Position, heardist, zmask); in my script.

So my intention was to simply draw a line from my object to layer 12 object. But my questions are:

Where did I go wrong in this programming?

More importantly when creating a Collider[] array I assume. Is there a array tutorial somewhere? Because I'm at a loss as to how i'm suppose to view the information inside other than in a for-loop. Even if I use a for-loop I don't know what information is returned, so its out of context.

Finally, In a similar application, using a var to capture the raycastall command to return a bunch of collided objects, how do I pick apart that variable, as if to say. Which object collided first? What position was it at? what rotation? which was the second object collided? The third? The last? These is all information that would be handy!

Thanks again. If you can only tackle one aspect that's fine. Awaiting enlightenment...

sgmongo

Comment
Add comment · Show 4
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 sgmongo · Oct 19, 2011 at 12:16 AM 0
Share

Bumpin for interest I could still use some expert helps :)

avatar image chemicalvamp · Oct 19, 2011 at 12:57 AM 0
Share

You can get an array tutorial all over google, they are quite generic :)

avatar image sgmongo · Oct 19, 2011 at 12:52 PM 0
Share

Found array tutorials, but printing the data in the collider array is causing unity to have a hissycow. Apparently I need to specify which bit of data is to be printed. Sadly the refrence guide doesn't say what data exactly is returned by a overlapshere. (I used some code from the googles to get a working overlapshere to test the array stuff... I still have no idea what i did wrong above)

Just a girl, still confused, in New Hampshire.

avatar image meat5000 ♦ · Aug 22, 2013 at 01:30 PM 0
Share

I know this is old but I was looking up Overlap function.

It fails because of the capital P

transform.Position should be transform.position

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by chemicalvamp · Apr 23, 2012 at 02:49 AM

Sorry this is in C#, but I'm sure you can make it work: using UnityEngine; using System.Collections;

 public class Test : MonoBehaviour
 {
     public float heardist = 50f;
     public int zmask = 1 << 12;
     public Vector3 stuff;
     public float damage = 10;
 
     void Update()
     {
 
         Collider[] colliders = Physics.OverlapSphere(transform.position, heardist, zmask);
         foreach (Collider col in colliders)
         {
             Debug.Log("I hit: " + col.name);
             stuff = col.transform.position;
             Debug.DrawLine(transform.position, stuff, Color.red);
 
             col.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
         }
     }
 }
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

Destroy objects within ordered Array by line of sight 1 Answer

Area of effect 1 Answer

Instantiate problem with multiple objects from an array 0 Answers

Physics2D.OverlapCircleAll and method call cause a huge drop in animation frames 1 Answer

Physics.OverlapSphere with a min radius? 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