Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How can I make a gameObject's color to a color in a gradient?

Discussion in '2D' started by BrewinNJuicin, Jul 27, 2022.

  1. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    Hello,
    I am making a game where you grow crops. I am currently trying to make the soil color when watered darker (gradually). I have tried to search how to gradually make the soil color darker, and it seems that I have to use a gradient. Thing is, I don't know how to use one, and I can't really understand the docs. Can someone help me out with this? Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    You call
    .Evaluate()
    on the gradient instance, passing in the position you want on the gradient, and it returns the Color at that point.

    What part of the example code provided in the docs are you struggling with?
     
    LuLusg and BrewinNJuicin like this.
  3. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    What I don’t understand about the example code in the docs are:
    The gradient = new Gradient() part
    The colorKey[#].time and colorKey[#].color parts
    Gradient.Evaluate[]
    And the alphaKey[#].alpha and alphaKey[#].time parts
    Also, may I have example code about the .Evaluate() part you said? I’m a beginner and I haven’t worked with that yet.
     
  4. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    136
    It sounds like you're struggling with the C# component of it. Are you familiar with C# coding? If so, you should at least understand how gradient = new Gradient() works.

    For the timing keys, it's simpler than it looks. Your gradient is created on a line starting at 0 and ending 1. You can change the color at different values along this line, such as 0.0 or 0.1 or 1.0 for example. And the gradient will blend the colors used at the different positions automatically. The difference between an alpha and a color key is that alpha affects transparency, whereas color is obvious.

    So GradientColorKey[] creates an array of type GradientColorKeys which stores both Color and Time values (from 0 - 1). The AlphaColorKey[] is an array of type AlphaColorKeys which stores both Alpha and Time values. Using these 2 components you can customize your gradient so maybe at position 0.0 you have a semi-transparent red, and at position 0.5 you have a more opaque blue, and at position 1.0 you have an opaque green.

    Hopefully that makes sense, but if you're struggling with the code I would recommend tackling C# as a programming language first before delving into example code in the docs. Best of luck!
     
    BrewinNJuicin likes this.
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    All that
    new
    stuff is because they are making a Gradient directly in code to prove the point.

    You would not normally need to make one like this.

    In your (and most typical) use case you make the Gradient field be public and you fill it out in the inspector.

    This means you only call .Evaluate() on it to read what the actual color should be.
     
    BrewinNJuicin likes this.
  6. BrewinNJuicin

    BrewinNJuicin

    Joined:
    Feb 25, 2022
    Posts:
    11
    Yes, I am familiar with C# coding. The part I was confused about was why they put it in there instead of not just making a public gradient and adjusting it in the inspector.

    Anyways, thank you both for your help! I have finally gotten the soil to get darker when it gets watered. This problem was bugging me for 2 or 3 days until I finally went on the forums to get a clear answer. I'm going to post the code below if anyone else stumbles on this thread with the same problem.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SoilManager : MonoBehaviour
    7. {
    8.     public float soilMoisture = 0;
    9.     private SpriteRenderer m_SpriteRenderer;
    10.     public Color color;
    11.     public Gradient gradient;
    12.  
    13.     private void Start()
    14.     {
    15.         m_SpriteRenderer = GetComponent<SpriteRenderer>();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         color = gradient.Evaluate(soilMoisture);
    21.         m_SpriteRenderer.color = color;
    22.     }
    23.  
    24.     private void OnParticleCollision(GameObject other)
    25.     {
    26.         Debug.Log("Particles Hit");
    27.  
    28.         if (soilMoisture < 1)
    29.         {
    30.             soilMoisture += .01f;
    31.         }
    32.         else
    33.         {
    34.             Debug.Log("Watered enough");
    35.             soilMoisture = 1;
    36.         }
    37.     }
    38.  
    39. }
    40.  
     
  7. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    Might I suggest taking those lines of code out of Update? I'm assuming you have this script on a lot of different objects, so in every single frame you are evaluating the gradient on every single object and then set the color on all of them. But in reality you only need to do that when the moisture value actually changes. Just putting those lines into the block that changes soilMoisture might improve performance by a lot.
     
    Chubzdoomer likes this.