Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to make different objects light up randomly for certain amount of times?

Discussion in 'Scripting' started by toanlam282, Nov 19, 2021.

  1. toanlam282

    toanlam282

    Joined:
    Nov 19, 2021
    Posts:
    3
    Hi, I am trying to figure out how to make my 3 objects light up randomly for a maximum of 5x per object. I am a beginner so I don't know how I would approach this. I would think that you need to make a collection, have a coroutine, and a random.range but I do not know how I would implement it. My script is pretty unorganized because I just pulled information from a bunch of different forums but maybe it would help. Thank you!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeColor : MonoBehaviour
    6. {
    7.     GameObject Top;
    8.     GameObject Middle;
    9.     GameObject Bottom;
    10.  
    11.     [SerializeField] private Material myMaterial;
    12.     [SerializeField] int numberToActivate;
    13.  
    14.  
    15.     List<GameObject> sphere = new List<GameObject>();
    16.  
    17.     private void Start()
    18.     {
    19.         List<GameObject> sphere = new List<GameObject>();
    20.  
    21.         sphere.Add(new GameObject("Top"));
    22.         sphere.Add(new GameObject("Middle"));
    23.         sphere.Add(new GameObject("Bottom"));
    24.  
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         if (Input.GetKeyDown(KeyCode.Space))
    30.         {
    31.             Debug.Log("Space has been pressed to light up");
    32.             StartCoroutine(ColorChange());
    33.         }
    34.     }
    35.    
    36.     private void callSpheres()
    37.     {
    38.         floor(Random.Range(0, List.Length - 1));
    39.     }
    40.  
    41.     private IEnumerator ColorChange()
    42.     {
    43.         myMaterial.color = Color.green;
    44.         yield return new WaitForSeconds(2f);
    45.         myMaterial.color = Color.white;
    46.     }
    47.  
    48.  
    49. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Well looks like you have made a pretty good attempt at a couple of things there... but I'm not sure I fully understand what you want to do.

    It looks like you have three spheres, you want them to stay some neutral color, except one at a time you want to stay a random color for 2 seconds? Then immediately pick another and color it for 2 seconds?

    Another part I don't quite get is the "maximum of 5x per object."
     
  3. Sam_Space

    Sam_Space

    Joined:
    Jun 28, 2019
    Posts:
    16
    In the Color Change function, you can pass a random gameobject(sphere) into it and set the material to the gameobject randomly selected.

    StartCoroutine(ChangeColor(callSpheres());

    private gameObject callSpheres()
    {
    return sphere[ floor(Random.Range(0, List.length - 1))];
    }

    private IEnumerator ColorChange(gameobject go)
    {
    go.GetComponent<Renderer>().material.color = Color.green;
    yield return new WaitForSeconds(2f);
    go.GetComponent<Renderer>().material.color = Color.white;
    }

    For each object, you might want a counter. So make a list of integers. When sphere #0 is chosen, add 1 count to it. If sphere #1 is randomly chosen then add 1 count. If the count is already total of 5 then dont change the material.

    Example:
    List<int> counters= new List<int>();
    counters.Add(0);
    counters.Add(0);
    counters.Add(0);

    within callSpheres change the script to this. It will loop, check the counter amount for each gameobject and if it's over 5 then the gameobject wont be returned and it will return a gameobject that still is below 5x. You can also add something that checks each counter beforehand to make sure if they are all maxed out to do something else.


    private gameObject callSpheres()
    {
    bool loop = true;

    while(loop == true){
    int i = Random.Range(0, List.length - 1)
    if(counter > 5){

    }else{
    return sphere;
    counter++;
    loop = false;
    }
    }
    }
     
  4. toanlam282

    toanlam282

    Joined:
    Nov 19, 2021
    Posts:
    3
    Yes, all three spheres start out with the same color and one at a time they change colors. The color itself is not random each time, let's just say they change from white to red randomly one at a time for a maximum of 5x per sphere. Once the sphere changes color to red, it stays red for 2 seconds then changes back to white for 2 seconds and is this is repeated randomly until all spheres have changed colors for a maximum of 5x. I appreciate your help!
     
  5. toanlam282

    toanlam282

    Joined:
    Nov 19, 2021
    Posts:
    3
    Hi, thank you for your time! I am a bit confused about the what is within callSpheres. So would I have both the bool loop = true... and the return sphere... functions in callSpheres or would they be in different functions. Also, could you go into more depth about the counters, I am not sure whether I should change my original list of "top", "middle", and "bottom" spheres or keep it with the counters. Thanks again for your help!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ChangeColor : MonoBehaviour
    6. {
    7.     GameObject Top;
    8.     GameObject Middle;
    9.     GameObject Bottom;
    10.  
    11.     [SerializeField] private Material myMaterial;
    12.     [SerializeField] int numberToActivate;
    13.  
    14.     List<GameObject> sphere = new List<GameObject>();
    15.  
    16.     private void Start()
    17.     {
    18.         List<GameObject> sphere = new List<GameObject>();
    19.  
    20.         sphere.Add(new GameObject("Top"));
    21.         sphere.Add(new GameObject("Middle"));
    22.         sphere.Add(new GameObject("Bottom"));
    23.  
    24.         List<int> counters = new List<int>();
    25.         counters.Add(0);
    26.         counters.Add(0);
    27.         counters.Add(0);
    28.     }
    29.  
    30.     void Update()
    31.     {
    32.         if (Input.GetKeyDown(KeyCode.Space))
    33.         {
    34.             Debug.Log("Space has been pressed to light up");
    35.             StartCoroutine(ColorChange(callSpheres));
    36.         }
    37.     }
    38.  
    39.     private GameObject callSpheres()
    40.     {
    41.         return sphere[floor(Random.Range(0, List.length - 1))];
    42.     }
    43.  
    44.     {
    45.         bool loop = true;
    46.  
    47.         while (loop == true)
    48.         {
    49.             int i = Random.Range(0, List.length - 1);
    50.         if (counter > 5)
    51.             {
    52.  
    53.             }
    54.             else
    55.             {
    56.                 return sphere;
    57.                 counter++;
    58.                 loop = false;
    59.             }
    60.         }
    61.     }
    62.  
    63.     private IEnumerator ColorChange(GameObject go)
    64.     {
    65.         go.GetComponent<Renderer>().material.color = Color.green;
    66.         yield return new WaitForSeconds(2f);
    67.         go.GetComponent<Renderer>().material.color = Color.white;
    68.     }
    69.  
    70. }