Home > Mobile >  Change GameObject color in unity (c#)
Change GameObject color in unity (c#)

Time:11-24

I'm new to unity and I'm trying to get a GameObject to change color when the game starts, but I get a error message saying " 'Color' does not contain a constructor that takes 4 arguments ", I've been trying to figure out what's wrong for 2 days, but I have no idea.

public class NewColor : MonoBehaviour
{
    private Renderer rend;

    [SerializeField]
    private Color colorToTurnTo = new Color (1f, 1f, 1f, 1f);

    void Start()
    {
        rend = GetComponent<Renderer>();

        rend.material.color = colorToTurnTo;
    }

I expected the GameObject to turn white when I started the game, but I couldn't start the game due to the errors.

CodePudding user response:

I'm not really sure, but maybe color should be capital because you define it as 'Color'.

What I mean is maybe you should go: rend.material.Color

I really don't know otherwise.

CodePudding user response:

As @hijinxbassist says, you must have confused System.Drawing and UnityEngine.

//using System.Drawing; // remove this line
using UnityEngine;

public class NewColor : MonoBehaviour
{
    private Color colorToTurnTo = new Color (1f, 1f, 1f, 1f);

    //...

See the documentation below, for more details: https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Color-ctor.html

  • Related