I have a cube, which consists of a lot of the little cubes. I want to increase scale value of each of this cubes, but for that I need to put each element to the public array in the inspector by hands, or work with a group of elements (but in this case effect wouldn’t so beautiful)
[48217-билет.jpg|48217]
What is the best way to select each object in the group in the script and increase it’s scale value?
2
This is going to be a little rough-looking, so just bear with me on this.
First, you’ll want an Editor Script. Therefore, you’ll need an “Editor” folder just inside the assets folder for your game. In there, make a script (whatever name you want, really. It won’t be compiled into the final game regardless). To use this, you’ll want to lock your selection in the inspector to your “Scripts” object, then select the cubes you want to add to the array in question.
In this script, you’ll want something along the lines of:
// C#
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Cube))]
public class CubeEditor : Editor
{
Cube CubeScript;
public override void OnInspectorGUI()
{
if(CubeScript == null)
{
CubeScript = (Cube)target;
}
if(GUILayout.Button("Set Cube Array"))
{
CubeScript.cubeArray = Selection.gameObjects;
}
if(GUILayout.Button("Add to Cube Array"))
{
GameObject[] newArray = new GameObject[CubeScript.cubeArray.Length + Selection.gameObjects.Length];
System.Array.Copy(CubeScript.cubeArray, newArray, CubeScript.cubeArray.Length); // VERY fast array copy
System.Array.Copy(Selection.gameObjects, 0, newArray, CubeScript.cubeArray.Length, Selection.gameObjects.Length);
CubeScript.cubeArray = newArray;
}
DrawDefaultInspector();
}
}
Note that this script contains no duplicate-checking, but this should work out as a starting point.
Edit: Whoops, forgot to actually add the objects to the array.
First you should give all your Cubes the same TAG, for example “Cube”. Then you can find them with a script and do what you want with them:
foreach(GameObject myCube in GameObject.FindGameObjectsWithTag("Cube"))
{
//Do Something
}