- Home /
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 ?
Answer by Glurth · Dec 16, 2016 at 04:17 PM
There are few different Selection members, I think you are just using the wrong one: https://docs.unity3d.com/ScriptReference/Selection.html
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).
Answer by imtzo · Jan 23, 2021 at 03:03 PM
This is what worked for me:
Selection.objects = new Object[] { gameObjectToSelect };
Selection.transforms
and Selection.gameObjects
are both read-only.
Answer by solutionlab · Jun 04, 2019 at 06:16 AM
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, Stefan
Your answer
Follow this Question
Related Questions
Fold/unfold gameobject from code 4 Answers
Editor Script Selection thinks Sprites are Texture2Ds. 2 Answers
Editor scripting, select a field 1 Answer
Select next object in Hierarchy? 0 Answers
Force select object in hierarchy via editor script? 1 Answer