• 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 dht1kna · Nov 28, 2010 at 02:14 PM · raycastparentchildrenraycollisions

Checking Child collisions through parents

Im learning unity right now. I made a Scene with four walls(Below): alt text

They are named Front wall,Back wall,Right wall,etc...

All Four walls are Children of an Empty game Object called "wall". In a script I use a ray to detect if those small sphere have collided with any of the walls through the Parent called "wall". It does not return the Debug message.

Here is the raycast code:

function Update () { var cast_col : RaycastHit; Debug.DrawRay(transform.position,Vector3(0,0,1)*3,Color.black);

 if (Physics.Raycast(transform.position,Vector3(0,0,1),cast_col,3))
 {
     if(cast_col.collider.gameObject.name == "wall")
     {
         Debug.Log("Hit!");
     }
 }

}

Whats wrong? Can i even check collision of all the children of a parent by checking collisions with the parent?

As you see I write in JavaScript so any code i would appreciate if it is in Java.

Thanks :D

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
0
Best Answer

Answer by ckfinite · Nov 28, 2010 at 03:27 PM

Basically, your problem is that you are getting the child gameobject name, and not the parent's. What you need is this:

if(cast_col.collider.gameObject.parent.name == "wall") { Debug.Log("Hit!"); }

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 dht1kna · Nov 29, 2010 at 12:39 PM 0
Share

I used this but it says : Object reference not set to an instance of an object

Here is the code i used: if (Physics.Raycast(transform.position,Vector3(0,0,1),cast_col,3)) { if(cast_col.collider.gameObject.parent.name == "wall") { Debug.Log("Hit!"); } }

avatar image ckfinite · Nov 30, 2010 at 02:31 AM 0
Share

Sorry, I got messed up. What you will need ins$$anonymous$$d is cast_col.collider.gameObject.transform.parent.gameObject.name

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to make an object the child of another object through code? 1 Answer

Parent an object to the camera via script? 1 Answer

Changing bool of raycasthit.parent 1 Answer

ray direction to quaternion 0 Answers

Childrens Add Force Command Not Working...,Parent and Child adding force question 0 Answers


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