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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Want to randomly change color of object every 2 seconds

Discussion in 'Scripting' started by anmartinezbenitez, Nov 10, 2022.

  1. anmartinezbenitez

    anmartinezbenitez

    Joined:
    Nov 10, 2022
    Posts:
    2
    Hello, I am trying to randomly change the color of a cube object every 2 seconds. As of right now, the colors are changing randomly, but I can't get them to change every 2 seconds. Instead, they are changing very rapidly. Way too quickly. Any help?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Cube : MonoBehaviour
    6. {
    7.    
    8.     public MeshRenderer Renderer;
    9.     private float startDelay = 2.0f;
    10.     private float repeat = 1000.0f;
    11.    
    12.     void Start()
    13.     {
    14.         transform.position = new Vector3(3, 4, 1);
    15.         transform.localScale = Vector3.one * 1.3f;
    16.        
    17.        
    18.     }
    19.    
    20.     void Update()
    21.     {
    22.  
    23.         InvokeRepeating("changeColor", startDelay, repeat);
    24.         transform.Rotate(10.0f * Time.deltaTime, 0.0f, 0.0f);
    25.     }
    26.  
    27.     void changeColor()
    28.     {
    29.         Material material = Renderer.material;
    30.        
    31.         material.color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
    32.  
    33.     }
    34. }
    35.  
     
    HamzaTopal likes this.
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You should only be calling InvokeRepeating once. You're calling it every frame.
     
  3. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    put
    Code (CSharp):
    1. InvokeRepeating("changeColor", startDelay, repeat);
    in start, then it gets called once
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Not a huge fan of black boxes like
    InvokeRepeating()
    .

    It provides virtually zero useful value in today's environments.

    Even worse when they accept a string for the function, which could have typos.

    Just use a counter.

    Code (csharp):
    1. private float yet;
    2. private float interval = 2.0f;
    ]

    and...

    Code (csharp):
    1. void Update()
    2. {
    3.   yet += Time.deltaTime;
    4.   if (yet >= interval)
    5.   {
    6.      yet -= interval;         // or set it to zero, your choice
    7.      changeColor();
    8.   }
    9. }
    And if you have a TON of these, wrap them up in a CallPeriodically class you make yourself, sorta like this CallAfterDelay() class, which could be trivially combined with the above code:

    https://gist.github.com/kurtdekker/0da9a9721c15bd3af1d2ced0a367e24e
     
    orionsyndrome likes this.
  5. anmartinezbenitez

    anmartinezbenitez

    Joined:
    Nov 10, 2022
    Posts:
    2
    Whoops! You're right. This fixed it. Thank you.