Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Unity rainbow color changing object

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

//Simple implementation of rainbow-colored object over time
//Cycles through colors as follows:
//White -> Yellow -> Red -> Black -> Blue -> Cyan -> White

//This example uses a cube as an example
public class Cube : MonoBehaviour
{
    public MeshRenderer Renderer;
    private Material material;
    private float red;
    private float green;
    private float blue;
    private float colorInterval;
    private bool whiteToBlack;

    Color baseColor;
    
    void Start()
    {
        red = 1.0f;
        blue = 1.0f;
        green = 1.0f;
        colorInterval = 0.005f; //Change this to a speed of your liking
        whiteToBlack = true;
        material = Renderer.material;
        baseColor = new Color(red, green, blue, 0.4f);
        material.color = baseColor;
    }
    
    void Update()
    {
        if (whiteToBlack)
        {
            if (blue > 0.0f)
                decreaseBlue();

            else if (green > 0.0f)
                decreaseGreen();

            else if (red > 0.0f)
                decreaseRed();

            else
                whiteToBlack = false;
        }

        if(!whiteToBlack)
        {
            if (blue < 1.0f)
                increaseBlue();

            else if (green < 1.0f)
                increaseGreen();

            else if (red < 1.0f)
                increaseRed();

            else
                whiteToBlack = true;
        }

        material.color = new Color(red, green, blue, 0.4f);

    }

    
    void decreaseRed()
    {
        red -= colorInterval;
    }

    void decreaseGreen()
    {
        green -= colorInterval;
    }

    void decreaseBlue()
    {
        blue -= colorInterval;
    }

    void increaseRed()
    {
        red += colorInterval;
    }

    void increaseGreen()
    {
        green += colorInterval;
    }

    void increaseBlue()
    {
        blue += colorInterval;
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to keep rigidbody2D upright unity 
Csharp :: c# remove time in datetime 
Csharp :: asp net img src path from database 
Csharp :: c# const vs readonly 
Csharp :: c# get set 
Csharp :: c# goto statement 
Csharp :: slither io hack 
Csharp :: class in c# 
Csharp :: delete all rows from table linq 
Csharp :: two linked list intersection 
Csharp :: serilog .net 6 
Csharp :: c# get a value from value tuple list 
Csharp :: cmd move directory to another directory 
Csharp :: remove duplicates in the list using linq 
Csharp :: render section asp.net mvc layout 
Csharp :: select range in list c# 
Csharp :: c# read xml tag value 
Csharp :: c# standard microphone decibels 
Csharp :: stock span problem c# using class 
Csharp :: How do I allow edit only a particular column in datagridview in windows application 
Csharp :: null-conditional operators c# 
Csharp :: conncet oracle database in c# visual studio 
Csharp :: how to create a blazor client-side application in a command-line interface 
Csharp :: how to check if an integer is in array c# 
Csharp :: if input.get touch 
Csharp :: c# on variable change property get set 
Csharp :: wpf binding ancestor codebehind 
Csharp :: example of List c# 
Csharp :: disable version header c# 
Csharp :: web.config customerrors not working 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =