Selection.objects question

I’m writing an editor script to select some objects.

In the docs for Selection.objects it says"

“The actual unfiltered selection. You can also assign objects to the selection.”

How do you “assign objects”?

I’ve tried creating an array with var newselection = new Array ();, then filling the array with Transforms and setting Selection.objects = newselection; but Unity “Cannot convert Array to UnityEngine.GameObject.”

So I assume I should not be using an array. What’s the proper method for adding objects to the current selection?

The two most interesting things to set in the Selection are:
Selection.activeGameObject. This gives the same effect as clicking an object in the Hierarchy view. I.e. the inspector shows the object and all attached components. (This is probably what you are looking for).

Selection.activeObject. Setting this to an object (e.g. a component) will show that object and only that object in the inspector. This can be handy if you have a object with a lot of components and only want to show one of them.

1 Like

var newselection = new Array (a, b, c, d).ToBuiltin(Object);

or

var newselection = [a, b, c, d];

I know that extra to builtin step is not very intuitive, we are working on fixing it.

Many thanks. So I can use either one of those two syntaxes? Do I have to know in advance how many items (4 in your example) will be selected? I see in the manual that builtins have a fixed size.

I don’t know in advance how many objects will be found, and I seem to be unable to modify the array.

I’ve declared the variable using the syntax you said, except I haven’t pre-loaded with 4 elements a, b, c, d.

The error I get is “Push is not a member of (Object).”

Do I need to convert to a builtin after the “search” is complete?

Maybe I just don’t yet get the connection between your code snippet and the action of “assigning objects.” I’ll play some more later and hopefully figure it out on my own, but FWIW, here’s the whole script (planned for the Wiki in case it helps others too):

import UnityEditor;

//Select all objects in the same horizontal plane (identical Y coordinate) as the currently active object. Useful for selecting one strata of a set of stacked objects, or all objects placed on a flat surface.

@MenuItem ("Custom/Select All In Plane")
static function SelectAllInPlane () {
	var plane = Selection.activeTransform.position.y;
	var allobjects = FindObjectsOfType(Transform);
	var newselection = []; 
	
	for (var eachobject : Transform in allobjects)
	{
		if (eachobject.position.y == plane)
		{
			newselection.Push(eachobject);
		}
	}
	Selection.objects = newselection;
}

OK–I’ve come up with the following script. I don’t know if I did the best thing, but the array errors are gone :slight_smile:

But now I’m getting an error message I can’t interpret. (This is in Assets/Editor/SelectAllInPlane.js). Upon launching Unity I get:

SelectAllInPlane.js(1) warning BCW0008: WARNING: Duplicate namespace: ‘UnityEditor’/

But that first line is just what the manual says to use.

Secondly, the script only selects ONE object instead of a whole array. Am I doing something wrong in the last two lines?

(How can I use print() to output the contents of an array, or other multi-line feedback? That would help me test.)

import UnityEditor;

//Select all objects in the same horizontal plane (Y coordinate within ±.005) as the currently active object. Useful for selecting one strata of a set of stacked objects, or all objects placed on a flat surface.

@MenuItem ("Custom/Select All In Plane")
static function SelectAllInPlane () {
	var tolerance = .005;
	var plane = Selection.activeTransform.position.y;
	var allobjects = FindObjectsOfType(Transform); 
	var newselection = new Array ();
	
	for (var eachobject : Transform in allobjects)
	{
		if (eachobject.position.y < plane+tolerance  eachobject.position.y > plane-tolerance)
		{
			newselection.Push(eachobject);
		}
	}
	
	var builtin : Transform[] = newselection.ToBuiltin(Transform);
	Selection.objects = builtin;
}

EDIT: removed a useless print line.

I just had a thought: you are setting the selection to a bunch of Transforms. Maybe if you set it to each object’s GameObject it’d work.

-Jon

I’ll try that and report back–this script might have other uses for someone someday. And if it works, it’s also a JavaScript example for people to play with.

FWIW, GameObject didn’t do it. I wonder if it’s some problem where the builtin array thinks it has a size of 1 and so only 1 object gets selected. But when I preset it to a size of [newselection.length] I just get an error.

Oh well, something I’ll play with again another day.