How can I select object in hierarchy in Unity via script?

private void CheckingSelection() {
Transform child = Selection.activeTransform;
Transform patchesTransform = builder.GetLevelEditorPatchesTransform();

    foreach (var parent in patchesTransform) {
        if (child.IsChildOf(parent) && child != parent) {
            Debug.Log("Set active " + parent.gameObject);
            Selection.activeGameObject = parent.gameObject;

That’s what I do, but it does not select the parent. What am I do wrong ?

There are few different Selection members, I think you are just using the wrong one:

I THINK you want to use Selection.transforms or Selection.gameObjects, rather than Selection.activeGameObject.

Note: both these functions take an ARRAY, rather than a single object (because one can select multiple objects at once).

This is what worked for me:

Selection.objects = new Object[] { gameObjectToSelect };

Selection.transforms and Selection.gameObjects are both read-only.

Hello Guys,
I found this question and think that @Vladislav-Hromyh exactly wanted to do the same as me…

I create a Button and a GameObject by clicking the mouse button. With this Button I want to activate/deactivate the created GameObject in Hierarchy. The thing is I don´t know how to link this two items (Button and GameObject) and select the GameObject in Hierarchy.

I have already looked at the link for docs.unity3d but it´s not described detailed enought for a “beginner” like me.

Can anybody help?

kind regards,