Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
How to change the colour of an object randomly every second in unity?

It's working now. This is the correct code. InvokeRepeating takes 3 arguments. I added the colors i needed and selected my mesh renderer

using UnityEngine;

public class colorchange : MonoBehaviour {

    public Color []colors;
    public Renderer rendering;

    void Start () 
    {
        InvokeRepeating ("ChangeColor", 0f, 1f);
    }

    void ChangeColor()
    {
        rendering.material.color = colors[Random.Range(0, colors.Length -1)];
    }

}

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

public Renderer renderer;

void Start()
{
     InvokeRepeating("ChangeColor", 1.0f, 1.0f);
}

void ChangeColor()
{
     renderer.material.color = new Color(Random.Range(0f,1f), Random.Range(0f,1f),  Random.Range(0f,1f), 1.0f);
}

One major issue is that it will give really "ugly" color in many occasions. So you'd be better off with a collection of color to choose from:

public Color []colors;
public Renderer renderer;

void Start()
{
     InvokeRepeating("ChangeColor", 1.0f, 1.0f);
}

void ChangeColor()
{
     renderer.material.color = colors[Random.Range(0, colors.Length -1)];
}

Some info source to look at: https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html https://docs.unity3d.com/ScriptReference/Renderer-material.html

over 4 years ago · Santiago Trujillo Report

0

  • You have to get the renderer component and use that to change it's material (rather than statically accessing the 'Renderer' class which will no work for your purpose).
  • When creating the new random color, you have to assign it to a variable, in your case 'newColor' because currently this variable doesn't exists in your script.
  • Random.value isn't a thing :)

The following code uses a property block to change the color of the renderer's material, avoiding the creation of a new material (for better performance, and I think better practice).

using UnityEngine;

[RequireComponent(Renderer)]
public class ColorChange : MonoBehaviour
{
    [SerializeField] private float _changeColorAfterSeconds = 1f;

    private Renderer _renderer;
    private float _timer = 0f;

    private MaterialPropertyBlock _block;

    private void Awake()
    {
        _renderer = GetComponent<Renderer>();
        _block    = new MaterialPropertyBlock();
    }
    
    private void Update ()
    {
        _timer += Time.deltaTime;

        if (_timer >= _changeColorAfterSeconds) 
        {
            Color newColor = new Color(Random.Range(0f, 1f),
                                       Random.Range(0f, 1f),
                                       Random.Range(0f, 1f),
                                       1f);

            _block.SetColor("_Color", newColor);
            _renderer.SetPropertyBlock(_block);

            _timer = 0f;
        }
    }
}

To simply fix your script / make it work as is:

using UnityEngine;

public class colorchange : MonoBehaviour {

    private float timer = 0f;

    void Update () {
        
        timer += Time.deltaTime;
        if (timer >= 1f) 
        {
            Color newColor = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), 1.0f);

            GetComponent<Renderer>().material.color = newColor;
            timer = 0f;
        }
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!