Selecting the child objects by ignoring its parent

So i followed up this tutorial

But i have some problem i am facing with I have tried everything to select the child object but it keep selecting the parent object. I have 320 children object under one parent. Does anyone know what i have done wrong?

// This is the mousemanager script

using UnityEngine;
using System.Collections;

public class MouseManager : MonoBehaviour {

    public GameObject selectedObject;

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
        Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );

        RaycastHit hitInfo;

        if( Physics.Raycast( ray, out hitInfo ) ) {
            //Debug.Log("Mouse is over: " + hitInfo.collider.name );

            // The collider we hit may not be the "root" of the object
            // You can grab the most "root-est" gameobject using
            // transform.root, though if your objects are nested within
            // a larger parent GameObject (like "All Units") then this might
            // not work.  An alternative is to move up the transform.parent
            // hierarchy until you find something with a particular component.

            GameObject hitObject = hitInfo.transform.root.gameObject;

            SelectObject(hitObject);
        }
        else {
            ClearSelection();
        }
  
    }

    void SelectObject(GameObject obj) {
        if(selectedObject != null) {
            if(obj == selectedObject)
                return;

            ClearSelection();
        }

        selectedObject = obj;

        Renderer[] rs = selectedObject.GetComponentsInChildren<Renderer>();
        foreach(Renderer r in rs) {
            Material m = r.material;
            m.color = Color.green;
            r.material = m;
        }
    }

    void ClearSelection() {
        if(selectedObject == null)
            return;

        Renderer[] rs = selectedObject.GetComponentsInChildren<Renderer>();
        foreach(Renderer r in rs) {
            Material m = r.material;
            m.color = Color.white;
            r.material = m;
        }


        selectedObject = null;
    }
}

The big comment there from lines 24 to 29 explains exactly what’s happening, and suggests what you might do about it.

Sorry JoeStrout i dont understand the sentence on 24-29. I have tried to make it work but when ever i hover my mouse on the object its selecting the parent. I just want to select the children rather than selecting its parent

It’s great that you’re following along with a tutorial, but I’m thinking you’re doing something that’s a bit more advanced than your current knowledge level still.

Once more, I’m going to recommend going through the Official Tutorials first, which will teach you not only how to get comfortable working with the IDE and writing scripts, but give you a better understanding of how Unity works with things like Transforms, parents, etc… Things that are important to understanding the script you’re currently working with.

Another important thing to learn is how to use the documentation. When you post code in script tags here on the forums, a bit of Internet magic happens and certain words become links that point right to their page in the docs! It’s super effective!

Checking out line 31, for example, you’ll see you’re using

hitInfo.transform.root.gameObject;

If you click on the word “root”, you’ll be shown that property’s page, which tells you that it returns the topmost Transform in the heirarchy. That seems to be exactly what you’re getting, right? It’s also exactly what that comment says will happen in your case. That’s what Joe was referring to.

So if you don’t want to get that object’s parent, how would you change that line of code to select the object you clicked instead of it’s parent?

1 Like

goodevening Schneider yea i am following exact what you told me before. i am learning but my english is big problem. i will try to find this out after getting my home. it seems i am becoming developer rather than doctor haha

Become both! Start a race of superhumans who will rule us all!

How about this?

GameObject hitObject = hitInfo.transform.GetChild(0).root.gameObject;

hitInfo.transform is the object that you clicked.

hitInfo.transform.root is the root of the hierarchy that contains the object clicked. You said you didn’t want that.

But now you’ve got hitInfo.transform.GetChild(0).root. That’d be the root of the hierarchy that contains the first child of the object clicked. That’d be exactly the same as the root of the object clicked. That’s what “root” means — the base of the tree of nested objects. So this version hasn’t accomplished anything at all.

hi JoeStrout what about this i think this one?

GameObject hitObject = hitInfo.transform.GetComponentInChildren.gameObject;

finally hahaha i am so stupid Just deleting the Parent simply fixed it happy as hell

1 Like

Sounds like you got it!

There is a temptation when you are new, and it all seems so confusing, to treat code as magic incantations, that you are expected to just invoke without understanding.

But that’s really not what programming is at all — it’s just a series of very precise, well-defined commands to the computer. You can read these commands, even when you’re new, and understand what they’re doing. And then it’s (usually) easy to change them to do what you want.

Just watch out… once you start to get the hang of it, programming is so much fun, you won’t want to do anything else!