Search code examples
c#unity-game-engine

I can't give transparency to gameobject in unity


I copied what this guy did https://www.youtube.com/watch?v=ZfWEBJVWydo

I wanted to give transparency to make it look good but "cannot modify the return value of 'material.color' because it is not a variable" occurred.

I also tried to give it a new color in RGB so it can have transparency, but still didn't work.

using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.ProBuilder.Shapes;
using Color = System.Drawing.Color;


public class Rainbow : MonoBehaviour
{
    private float effsped = 15;

    private float hue;
    private float sat=0.68f;
    private float bri = 1;
    private MeshRenderer MR;
    Color32 objcolor;
    // Start is called before the first frame update
    void Start()
    {
        MR = GetComponent<MeshRenderer>();
        hue = Random.Range(0f, 1f);
        MR.material.color = UnityEngine.Color.HSVToRGB(hue, sat, bri);


    }

    // Update is called once per frame
    void Update()
    {
        UnityEngine.Color.RGBToHSV(MR.material.color, out hue, out sat, out bri);
        hue += (effsped / 10000);
        if (hue >= 0.99f)
        {
            hue = 0f;
        }
        MR.material.color = UnityEngine.Color.HSVToRGB(hue, sat, bri, true);
        Color hastrancy = new Color(MR.material.color.r, MR.material.color.g, MR.material.color.b, 0.8f);
        MR.material.SetColor("_Color", hastrancy);

    }
}
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using Color = System.Drawing.Color;


public class Rainbowinv : MonoBehaviour
{
    private float effsped = 15;

    private float hue;
    private float sat=0.68f;
    private float bri = 1;
    private MeshRenderer MR;
    // Start is called before the first frame update
    void Start()
    {
        MR = GetComponent<MeshRenderer>();
        hue = Random.Range(0f, 1f);
        MR.material.color = UnityEngine.Color.HSVToRGB(hue, sat, bri);

    }

    // Update is called once per frame
    void Update()
    {
        UnityEngine.Color.RGBToHSV(MR.material.color, out hue, out sat, out bri);
        hue -= (effsped / 10000);
        if (hue <= 0)
        {
            hue = 0.99f;
        }
        MR.material.color = UnityEngine.Color.HSVToRGB(hue, sat, bri) ;
        MR.material.color.a = 0.8f;
    }
}

Those two are components of each two gameobjects and these are errors I have:

Assets\Scripts\Rainbowinv.cs(35,9): error CS1612: Cannot modify the return value of 'Material.color' because it is not a variable

How do I get the RGB value of a material in unity?

MR.material.color.a is same with objColor. But why did this answer work?

Assets\Scripts\Rainbow.cs(38,31): error CS1729: 'Color' does not contain a constructor that takes 4 arguments

https://docs.unity3d.com/ScriptReference/Material.SetColor.html

Then why is this working?

Assets\Scripts\Rainbow.cs(39,40): error CS1503: Argument 2: cannot convert from 'System.Drawing.Color' to 'UnityEngine.Color'

wth does this even mean? I thought using Color = System.Drawing.Color; is a sentence that prevent error, but now it`s causing another error!


Solution

  • You cannot edit the fields of Material.color directly, you can only assign a new value to it. If you want to set a single component, a in your case, you would have to create a new Color variable, change the values, then assign the new color to your material:

    Color newColor = UnityEngine.Color.HSVToRGB(hue, sat, bri);
    newColor.a = 0.8f;
    MR.material.color = newColor;
    

    Also, you should not be using Color = System.Drawing.Color; here. This changes all references to "Color" in that file to refer to the built-in C# "Color" struct, not Unity's custom "Color" struct (which Material.color expects).

    This is the source of the cannot convert from 'System.Drawing.Color' to 'UnityEngine.Color' errors.