- Home /
Detect collision with child object?
Hi I am making my first game with unity and I have a script to crush the player. In this script I also have a collision detecting function.
here is my script:`using System.Collections;
using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class CrushingThePlayer : MonoBehaviour {
public bool isCollidedWithWall = false;
public bool isCollidedWithCube = false;
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Wall")
isCollidedWithWall = true;
else if (collision.gameObject.name == "MovaeableCube")
isCollidedWithCube = true;
if (isCollidedWithWall && isCollidedWithCube)
SceneManager.LoadScene("Scene1");
}
public void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "Wall")
isCollidedWithWall = false;
else if (collision.gameObject.name == "MoveableCube")
isCollidedWithCube = false;
}
}
I want the same script, but with a child that detects the collision. I now have if (collision.gameObject.name == "MoveableCube") but want to do it with a child of the cube
if someone knows how to do this please tell me.
Answer by $$anonymous$$ · Feb 27, 2021 at 04:27 PM
You can try
if (collision.gameObject.transform.GetChild(0).name == "MovaeableCube") {
isCollidedWithCube = true;
}
if you do not know transform.GetChild take in the index of the child in inspector it starts from 0.
Hope this helps
if you do not know what transform.GetChild() is
thank you for the very detailed comment. but I still have a problem. Now my script has this error
UnityException: Transform child out of bounds CrushingThePlayer.OnCollisionEnter (UnityEngine.Collision collision)
my script looks like this:
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class CrushingThePlayer : MonoBehaviour
{
public bool isCollidedWithWall = false;
public bool isCollidedWithCube = false;
public GameObject MoveableCube;
private void Awake()
{
for (int i = 0; i < MoveableCube.transform.childCount; i++)
{
Transform t = MoveableCube.transform.GetChild(i);
Debug.Log("cube child: " + t.name);
}
}
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "TopWall")
isCollidedWithWall = true;
else if (collision.gameObject.transform.GetChild(2).name == "MovaeableCube")
{
isCollidedWithCube = true;
}
if (isCollidedWithWall && isCollidedWithCube)
SceneManager.LoadScene("Scene1");
}
public void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == "TopWall")
isCollidedWithWall = false;
else if (collision.gameObject.transform.GetChild(2).name == "MovaeableCube")
{
isCollidedWithCube = true;
}
}
}*
can you help me? did I forget something? thanks for your help
Answer by nursedayuksel · Feb 27, 2021 at 04:39 PM
gameObject.transform.GetChild(int index)
does exactly that. 'index' stands for the index of the child/childrenren of the game object. So in your case, if you only have one child, then you'd do:
if (collision.gameObject.transform.GetChild(0).name == "MovaeableCube")
{
isCollidedWithCube = true;
}
The 0 in GetChild(0)
means that the child of the game object you want to access to is the first child of that game object.
so would the "MoveableCube" in this example be the name of the child or the name of the parent?
Your answer
Follow this Question
Related Questions
FindChild/Change color 1 Answer
ChildCount number returns incorrect 1 Answer
Switch for children not working 1 Answer
how to access scripts in children 2 Answers