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 $$anonymous$$ · Feb 27, 2021 at 04:03 PM · collision detectionchildchildrenchild object

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.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by $$anonymous$$ · Feb 27, 2021 at 04:27 PM

You can try

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Feb 27, 2021 at 04:28 PM 0
Share
 if (collision.gameObject.transform.GetChild(0).name == "MovaeableCube") {
         isCollidedWithCube = true;
 }
avatar image $$anonymous$$ · Feb 27, 2021 at 04:29 PM 0
Share

if you do not know transform.GetChild take in the index of the child in inspector it starts from 0.

Hope this helps

avatar image $$anonymous$$ · Feb 27, 2021 at 04:31 PM 0
Share

if you do not know what transform.GetChild() is

transform.GetChild()

avatar image $$anonymous$$ · Mar 03, 2021 at 07:25 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Show 1 · 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 $$anonymous$$ · Feb 28, 2021 at 03:46 PM 0
Share

so would the "MoveableCube" in this example be the name of the child or the name of the parent?

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

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Foreach loop trying to grab all children in scene 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