Why do colors stop changing from the totalTimer = 300 +/-

Asked

Viewed 38 times

1

Basically with this code I’m trying to make the color and emission color of the object change systematically at a predetermined time, but whenever the totalTimercomes around the value of 300 colors basically stop changing. I am using the 2019.2.5f1 version of Unity for this project. So someone knows what’s going on?

P.S.: One thing I noticed was that every time I change the value of frameTimer for higher values the value at which the totalTimer changes too, but how I’m using the frameTimer as a frame counter, it would not be beneficial to have a value above 60.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColorChanger : MonoBehaviour
{
    #region State Variables
    //state variables
    private int frameTimer;
    private int totalTimer = 0;
    private int newSpeed;
    private int colorChanger;
    #endregion

    #region Properties
    public int NewSpeed
    {
        get => newSpeed;
    }
    #endregion

    Renderer rend;

    public Color[] colors;
    private Color newColor, oldColor;


    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        newSpeed = 40;
        #region Color instantion
        colors = new Color[9];
        colors[0] = (new Color(0.5098f, 0.3254f, 0.1882f));    //brown
        colors[1] = (new Color(0.8941f, 0.9137f, 0.0392f));    //yellow 
        colors[2] = (new Color(0.1607f, 0.8705f, 0.9568f));    //light blue
        colors[3] = (new Color(1f, 0.5215f, 0.0745f));         //orange
        colors[4] = (new Color(0.7607f, 0.2274f, 0.8588f));    //lightish purple
        colors[5] = (new Color(0.3254f, 0.8588f, 0.2274f));    //green
        colors[6] = (new Color(0.9568f, 0.2470f, 0.1607f));    //red
        colors[7] = (new Color(0.7294f, 0.1490f, 0.1450f));    //crimson
        colors[8] = (new Color(0.0705f, 0.3960f, 0.9019f));    //blue
        #endregion
    }

    // Update is called once per frame
    void Update()
    {
        frameTimer++;
        colorChanger++;

        if (frameTimer == 30)
        {
            SpeedChanger(totalTimer++); frameTimer = 0;
           Debug.Log($"The Total Timer is {totalTimer}");
        }

        #region Color Update
        if(colorChanger == newSpeed)
        {
            newColor = colors[Random.Range(0, colors.Length - 1)];

            if(newColor == oldColor)
            {
                while(newColor == oldColor)
                    newColor = colors[Random.Range(0, colors.Length - 1)];
            }
            oldColor = newColor;
            rend.material.SetColor("_Color", newColor);
            rend.material.SetColor("_EmissionColor", newColor);
            rend.material.EnableKeyword("_EMISSION");
            colorChanger = 0;
            //Debug.Log( $"\n {newColor} this is the frame color");
        }
        #endregion
    }

    private int SpeedChanger(int totalTimer)
    {
        switch(totalTimer)
        {
            case 85:
                newSpeed = 30;
                break;
            case 165:
                newSpeed = 25;
                break;
            case 200:
                newSpeed = 20;
                break;
            case 300:
                newSpeed = 15;
                break;
        }
        return newSpeed; 
    }
}

1 answer

1

It happens because the moment you change the newSpeed calling the Speedchanger(int totalTimer) method your colorChanger is bigger than fifteen and it will be zero because for that the collorChange has to be equal to newSpeed.

But honestly, that’s not the best way to do it. The best would be to make a counter using the time.deltatime, until like this the speed that will change the color of your object will depend on the clock of the machine that is running.

It would look something like this

#region State Variables
    //state variables
    private float howManyChanges;
    private float speedToChange;
    private float timer = 0;
    private int lastRandomColorIndex;
    public Color[] colors;
    #endregion

    Renderer rend;



    // Start is called before the first frame update
    void Start()
    {
        rend = GetComponent<Renderer>();
        speedToChange = 5;
        #region Color instantion
        colors = new Color[9];
        colors[0] = (new Color(0.5098f, 0.3254f, 0.1882f));    //brown
        colors[1] = (new Color(0.8941f, 0.9137f, 0.0392f));    //yellow 
        colors[2] = (new Color(0.1607f, 0.8705f, 0.9568f));    //light blue
        colors[3] = (new Color(1f, 0.5215f, 0.0745f));         //orange
        colors[4] = (new Color(0.7607f, 0.2274f, 0.8588f));    //lightish purple
        colors[5] = (new Color(0.3254f, 0.8588f, 0.2274f));    //green
        colors[6] = (new Color(0.9568f, 0.2470f, 0.1607f));    //red
        colors[7] = (new Color(0.7294f, 0.1490f, 0.1450f));    //crimson
        colors[8] = (new Color(0.0705f, 0.3960f, 0.9019f));    //blue
        #endregion
        NewRandomColor();
    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;

        if (timer >= speedToChange)
        {
            howManyChanges++;
            SpeedChanger();
            NewRandomColor();
            timer = 0;
        }
    }

    private void NewRandomColor()
    {
        int randomColorIndex = Random.Range(0, colors.Length - 1);
        if(lastRandomColorIndex == randomColorIndex)
        {
            if (randomColorIndex == colors.Length -1)
            {
                randomColorIndex--;
            }
            else
            {
                randomColorIndex++;
            }
        }

        lastRandomColorIndex = randomColorIndex;
        Color newColor = colors[Random.Range(0, colors.Length - 1)];
        rend.material.SetColor("_Color", newColor);
        rend.material.SetColor("_EmissionColor", newColor);
        rend.material.EnableKeyword("_EMISSION");
    }

    private void SpeedChanger()
    {
        switch (howManyChanges)
        {
            case 5: //Quantas vezes já trocou
                speedToChange = 4; //Segundos para proxima troca
                break;
            case 10:
                speedToChange = 3; //Segundos para proxima troca
                break;
            case 15:
                speedToChange = 2; //Segundos para proxima troca
                break;
            case 20:
                speedToChange = 1; //Segundos para proxima troca
                break;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.