Add a Transform/GameObject to the selection from an editor script


I’m making a kind of level editor using a JavaScript unity editor script. I wan’t to be able to add a transform/gameobject to the selection array. I’ve tried using Selection.transforms and .objects but I keep getting Cast Exceptions. What is the best way to do this? My project won’t work without it.

Here’s my code as is

var clicked = GetGameObjectAtPoint(lastMouseUp);
//Convert to JS array
var a = new Array(Selection.objects);
//Convert back to Builtin array
Selection.objects = a.ToBuiltin(UnityEngine.Object);

Thanks! :slight_smile:

  • James

Selection.transforms is defined as
static var transforms : Transform

which is not dynamically resizable, so you’d either need to convert to/from Array or allocate a larger Transform and assign it back to Selection.transforms. That said, I don’t know if that will work or not.

a different option to using Select to manipulate objects is to just add selections to prefabs to put them in a kind of memory you can re-use, and if you want to select stuff with the mouse and mod their attributes, you can make a tag called :select: and whatever objects you select get that tag, adn then you do take all objects tagged select and do whatever you wanted to them. did you get this working to add selections in code?