Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

420
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda