All index List Help-me

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

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

[System.Serializable]
public class CostumizeAction
{
    public string CostumizeName;
    public GameObject[] Costumizes;
}

public class CostumizeStart : MonoBehaviour {

    public List<CostumizeAction> CostumizeParamers;

    // Use this for initialization
    void Start () {


        int random = Random.Range (0, CostumizeParamers[AllIndex].Costumizes.Length);
        CostumizeParamers[AllIndexs].Costumizes [random].SetActive (true);

    }
 
    // Update is called once per frame
    void Update () {


    }

}

Use code tags when posting code.

The basic thing you’re looking for here is a for loop:

for (int i=0; i<CostumizeParamers.Count; i++) {
CostumizeParamers[i].Costumizes [random].SetActive (true);
}

and random?

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.

what?

            for (int i = 0; i < CostumizeParamers.Count; i++)
            {
                int random = Random.Range(0, CostumizeParamers[i].Costumizes.Length);
                CostumizeParamers[i].Costumizes[random].SetActive(true);
            }

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.

1 Like