How can i add properties for each element in array of gameobject ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//[ExecuteInEditMode]
public class InstantiateObjects : MonoBehaviour
{
   public GameObject[] objectsToInstantiate;
   public Terrain terrain;
   public float yOffset = 0.5f;
   public int numberOfObjectsToCreate;
   public bool parent = true;
   public bool randomScale = false;
   public float setRandScaleXMin, setRandScaleXMax;
   public float setTandScaleYMin, setTandScaleYMax;
   public float setTandScaleZMin, setRandScaleZMax;

   private float terrainWidth;
   private float terrainLength;
   private float xTerrainPos;
   private float zTerrainPos;
   private GameObject objInstance;
   private GameObject[] createdObjects;
   private string objname;

   public void Start()
   {
       //Get terrain size
       terrainWidth = terrain.terrainData.size.x;
       terrainLength = terrain.terrainData.size.z;

       //Get terrain position
       xTerrainPos = terrain.transform.position.x;
       zTerrainPos = terrain.transform.position.z;

       for(int i = 0; i < objectsToInstantiate.Length; i++)
       {
           objname = objectsToInstantiate[i].name;
           MyCustomEditor.TagsAndLayers.AddTag(objname);
       }

       generateObjectOnTerrain();
   }

   public void Update()
   {

   }

   public void DestroyObjects()
   {
       UpdateList(true);

       if (createdObjects != null && createdObjects.Length > 0)
       {
           for (int i = 0; i < createdObjects.Length; i++)
           {
               DestroyImmediate(createdObjects[i]);
           }
           createdObjects = new GameObject[0];
       }
   }

   public void generateObjectOnTerrain()
   {
       for (int i = 0; i < objectsToInstantiate.Length; i++)
       {
           //Generate random x,z,y position on the terrain
           float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
           float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);

           float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));

           //Generate random x,y,z scale on the terrain
           float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
           float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
           float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);

           //Apply Offset if needed
           yVal = yVal + yOffset;

           //Generate the Prefab on the generated position      
           objInstance = Instantiate(objectsToInstantiate[i], new Vector3(randX, yVal, randZ), Quaternion.identity);

           if (randomScale == true)
               objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);

           if (parent)
               objInstance.transform.parent = this.transform;

           objInstance.tag = objname;
       }

       createdObjects = GameObject.FindGameObjectsWithTag(objname);

       if (objname == "Teleportation Booth")
           UpdateList(false);
   }

   private void UpdateList(bool destroy)
   {
       GameObject go = GameObject.Find("Main Camera");
       var list = go.GetComponent<PatrolOverTerrain>().Targets;
       if (destroy == false)
       {
           list.AddRange(createdObjects);
           go.GetComponent<PatrolOverTerrain>().Targets = list;
       }

       if (destroy == true)
       {
           list.Clear();
           go.GetComponent<PatrolOverTerrain>().Targets.Clear();
       }
   }
}

Instead attaching the script to every gameobject i will want to clone i want to use array of objects.

For example let’s say i have in the array 2 objects now i want in the inspector to have under each element it’s own properties. And when i will set the properties as children for each element it will take effect for the specific element only.

Instead general properties for all the elements to make this properties from Y Offset until the last Set Rand Scale Z Max to be children for each element. So i can set the properties per element.

So i can expand each element and see his properties.

Btw: How can i change the random for example that: Set Rand Scale X Min and Set Rand Scale X Min will be on one line and not two lines ? Like:

Set Rand Scale X Min Max Set Rand Scale Y Min Max Set Rand Scale Z Min Max

I don’t think you can expand a game object to see its properties. Maybe a custom drawer could be used to expand an object if it had a certain script you want to look at (I don’t know for sure, but sounds plausible).

If you’re talking about the same line in the inspector, you could make those Rand Scale X,Y,Z a Vector3 and it would show up on 1 line, if that’s what you were asking.

1 Like