Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

427
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda