Search Unity

All index List Help-me

Discussion in 'Scripting' started by juniorforestS, Apr 28, 2017.

  1. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47


    I'm trying to make only one object from all lists randomly active
    Help-me


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [System.Serializable]
    7. public class CostumizeAction
    8. {
    9.     public string CostumizeName;
    10.     public GameObject[] Costumizes;
    11. }
    12.  
    13. public class CostumizeStart : MonoBehaviour {
    14.  
    15.     public List<CostumizeAction> CostumizeParamers;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.  
    20.  
    21.         int random = Random.Range (0, CostumizeParamers[AllIndex].Costumizes.Length);
    22.         CostumizeParamers[AllIndexs].Costumizes [random].SetActive (true);
    23.  
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update () {
    28.  
    29.  
    30.     }
    31.  
    32. }
    33.  
     
    Last edited: Apr 28, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,776
    Use [code ]code tags[/code] when posting code.

    The basic thing you're looking for here is a for loop:
    Code (csharp):
    1. for (int i=0; i<CostumizeParamers.Count; i++) {
    2. CostumizeParamers[i].Costumizes [random].SetActive (true);
    3. }
     
  3. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
    and random?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,776
    Oh, I misunderstood the point of the loop. You can move the "random" line into the for loop as well, and likewise use i in place of allindexes.
     
  5. juniorforestS

    juniorforestS

    Joined:
    Apr 18, 2015
    Posts:
    47
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,501
    Code (csharp):
    1.             for (int i = 0; i < CostumizeParamers.Count; i++)
    2.             {
    3.                 int random = Random.Range(0, CostumizeParamers[i].Costumizes.Length);
    4.                 CostumizeParamers[i].Costumizes[random].SetActive(true);
    5.             }
    This is assuming that all other items are OFF. In the event that you ever need to change the .. "Cosumizes".. at runtime you'll need a better loop that toggles everything off except for the chosen index - which, hopefully after this you can see how that would work in a loop as you have control over the index and the chosen random value.
     
    juniorforestS likes this.