Search Unity

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

Discussion in 'Scripting' started by Chocolade, May 27, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. //[ExecuteInEditMode]
    7. public class InstantiateObjects : MonoBehaviour
    8. {
    9.    public GameObject[] objectsToInstantiate;
    10.    public Terrain terrain;
    11.    public float yOffset = 0.5f;
    12.    public int numberOfObjectsToCreate;
    13.    public bool parent = true;
    14.    public bool randomScale = false;
    15.    public float setRandScaleXMin, setRandScaleXMax;
    16.    public float setTandScaleYMin, setTandScaleYMax;
    17.    public float setTandScaleZMin, setRandScaleZMax;
    18.  
    19.    private float terrainWidth;
    20.    private float terrainLength;
    21.    private float xTerrainPos;
    22.    private float zTerrainPos;
    23.    private GameObject objInstance;
    24.    private GameObject[] createdObjects;
    25.    private string objname;
    26.  
    27.    public void Start()
    28.    {
    29.        //Get terrain size
    30.        terrainWidth = terrain.terrainData.size.x;
    31.        terrainLength = terrain.terrainData.size.z;
    32.  
    33.        //Get terrain position
    34.        xTerrainPos = terrain.transform.position.x;
    35.        zTerrainPos = terrain.transform.position.z;
    36.  
    37.        for(int i = 0; i < objectsToInstantiate.Length; i++)
    38.        {
    39.            objname = objectsToInstantiate[i].name;
    40.            MyCustomEditor.TagsAndLayers.AddTag(objname);
    41.        }
    42.  
    43.        generateObjectOnTerrain();
    44.    }
    45.  
    46.    public void Update()
    47.    {
    48.  
    49.    }
    50.  
    51.    public void DestroyObjects()
    52.    {
    53.        UpdateList(true);
    54.  
    55.        if (createdObjects != null && createdObjects.Length > 0)
    56.        {
    57.            for (int i = 0; i < createdObjects.Length; i++)
    58.            {
    59.                DestroyImmediate(createdObjects[i]);
    60.            }
    61.            createdObjects = new GameObject[0];
    62.        }
    63.    }
    64.  
    65.    public void generateObjectOnTerrain()
    66.    {
    67.        for (int i = 0; i < objectsToInstantiate.Length; i++)
    68.        {
    69.            //Generate random x,z,y position on the terrain
    70.            float randX = UnityEngine.Random.Range(xTerrainPos, xTerrainPos + terrainWidth);
    71.            float randZ = UnityEngine.Random.Range(zTerrainPos, zTerrainPos + terrainLength);
    72.  
    73.            float yVal = Terrain.activeTerrain.SampleHeight(new Vector3(randX, 0, randZ));
    74.  
    75.            //Generate random x,y,z scale on the terrain
    76.            float randScaleX = Random.Range(setRandScaleXMin, setRandScaleXMax);
    77.            float randScaleY = Random.Range(setTandScaleYMin, setTandScaleYMax);
    78.            float randScaleZ = Random.Range(setTandScaleYMax, setRandScaleZMax);
    79.  
    80.            //Apply Offset if needed
    81.            yVal = yVal + yOffset;
    82.  
    83.            //Generate the Prefab on the generated position      
    84.            objInstance = Instantiate(objectsToInstantiate[i], new Vector3(randX, yVal, randZ), Quaternion.identity);
    85.  
    86.            if (randomScale == true)
    87.                objInstance.transform.localScale = new Vector3(randScaleX, randScaleY, randScaleZ);
    88.  
    89.            if (parent)
    90.                objInstance.transform.parent = this.transform;
    91.  
    92.            objInstance.tag = objname;
    93.        }
    94.  
    95.        createdObjects = GameObject.FindGameObjectsWithTag(objname);
    96.  
    97.        if (objname == "Teleportation Booth")
    98.            UpdateList(false);
    99.    }
    100.  
    101.    private void UpdateList(bool destroy)
    102.    {
    103.        GameObject go = GameObject.Find("Main Camera");
    104.        var list = go.GetComponent<PatrolOverTerrain>().Targets;
    105.        if (destroy == false)
    106.        {
    107.            list.AddRange(createdObjects);
    108.            go.GetComponent<PatrolOverTerrain>().Targets = list;
    109.        }
    110.  
    111.        if (destroy == true)
    112.        {
    113.            list.Clear();
    114.            go.GetComponent<PatrolOverTerrain>().Targets.Clear();
    115.        }
    116.    }
    117. }
    118.  
    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
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
    Chocolade likes this.