Referencing a child

Hello!

I am making a project where if player collides with any child of “Asteroids”, Application.Loadlevel(“lose”) needs to run. However, I cannot find a way to reference the child without getting an error.

I would appreciate any help!
Thanks!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collision : MonoBehaviour {

private void OnCollisionEnter2D(Collision2D collision) {
    if (collision.gameObject.name == "Asteroids") {
        Application.LoadLevel("Lose");

    }

} 

}

Let me try to understand: in the scene you have a GameObject called “Asteroids” that has asteroids as children (and all of them have their own collider). And you wanna know when you’ve collided with any them?

Instead of trying to check for their name why don’t you use tags?
In the inspector, below the gameobject name you can set the tag.

Then you’d simply use:

 private void OnCollisionEnter2D(Collision2D collision) {
     if (collision.gameObject.tag.Equals("Asteroid")) {
         Application.LoadLevel("Lose");
     }
 } 

(Where “Asteroid” is a tag all asteroids share)

For those of you who want to know how to Refrence a child of a GameObject.
The following links should help.

Transform.GetChild ScriptingAPI

access child of GameObject fourms