Search Unity

Identify selected variable from array

Discussion in 'Scripting' started by Alp-Giray-Savrum, Jul 20, 2013.

  1. Alp-Giray-Savrum

    Alp-Giray-Savrum

    Joined:
    Nov 24, 2012
    Posts:
    13
    Code (csharp):
    1.      //Thoose are objects that i have;
    2.      
    3.      
    4.     var shooterPlatform : GameObject;
    5.     var wireFrame : GameObject;
    6.     var passObj : GameObject;
    7.     var bareels : GameObject;
    8.      
    9.      
    10.     var randomObject : Array;
    11.      
    12.      
    13.      
    14.      
    15.     function Awake()
    16.     {
    17.     //--------------------------------------------------------------------------
    18.      
    19.     //At there, Instantiate the object that selected randomly
    20.      
    21.     randomObject = new Array(shooterPlatform, wireFrame, passObj, bareels);
    22.      
    23.     }
    24.      
    25.     function Start () {
    26.      
    27.     var randomSelect = Random.Range(0,(randomObject.length)); //since you want to get a random object every time
    28.      
    29.      
    30.     // At there, if selected variable's name from array list is wireFrame,
    31.     // Applicate this code, if another else like, shooterPlatform, apply another code i wrote.
    32.     // I can't find how to get variable name from array list :(
    33.      
    34.     var position = transform.TransformPoint(Random.Range(16.47816, -1.048871), 0, Random.Range(16.47816, -1.048871));
    35.      
    36.      
    37.     Instantiate(randomObject[randomSelect], position, Quaternion.identity);
    38.      
    39.     }
    I don't know how to read selected variable can any body help ?
     
  2. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Your array contains Objects, not variables, but if you just want the name of the object, you can get that via the name property (yourObject.name).

    See the example below. And note that JavaScript arrays are slow and untyped, so I converted it to a .NET array of GameObjects.

    Code (csharp):
    1. var shooterPlatform : GameObject;
    2. var wireFrame : GameObject;
    3. var passObj : GameObject;
    4. var bareels : GameObject;
    5. private var myObjects : GameObject[];
    6.  
    7. function Awake(){
    8.     myObjects = [shooterPlatform, wireFrame, passObj, bareels];
    9. }
    10.  
    11. function Start() {
    12.     var randomObject = myObjects[Random.Range(0,myObjects.Length)];
    13.     Debug.Log(randomObject.name);
    14. }
    15.