- Home /
Checking Child collisions through parents
Im learning unity right now. I made a Scene with four walls(Below):
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
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!"); }
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!"); } }
Sorry, I got messed up. What you will need ins$$anonymous$$d is cast_col.collider.gameObject.transform.parent.gameObject.name
Your answer
Follow this Question
Related Questions
Changing bool of raycasthit.parent 1 Answer
How to make an object the child of another object through code? 1 Answer
Parent an object to the camera via script? 1 Answer
iOS Children's Game: Closing and relaunching again allows user to access the App Store 0 Answers
Someone can help with Ray/Collide? 1 Answer