简体   繁体   中英

Unity3d - Trying to change color of Material via C#

I've looked all over for a solution for this and it seems simple enough but I don't understand why I can't change the property of one of the materials located on one of my NPC models.

To put it simple - I am just trying to change the "_SkinColor" property of what I believe is the material shader? I guess because using the code shown will change the skin color to black?

Thank you so much for your help!

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


public class colorchanger : MonoBehaviour
{
    [SerializeField] private Material myMaterial;

    //    [SerializeField]

    
    private float red = Random.Range(180, 231);
    private float green = Random.Range(200, 255);
    private float blue = Random.Range(50, 100);


    // Start is called before the first frame update
    void Start()
    {
        Color newColor = new Color(red,green,blue);
        myMaterial.SetColor("_SkinColor",newColor);
    }

    // Update is called once per frame
    void Update()
    {
    
    }

}

You will need to set the color of the material via its Renderer component.

If the script is attached to the GameObject that has the renderer which is handling your material try:

void Start()
{
    Renderer myRenderer = GetComponent<Renderer>();
    Color newColor = new Color(red,green,blue);
    myRenderer.material.color = newColor;
}

If the script is intended to change the colour of whatever material is passed in to it, you need to pass in the relevant Renderer rather than the material. For example:

public class MaterialColourChanger: MonoBehaviour {
    
    public void ChangeMaterialColour(Renderer renderer, Color colour) {
        renderer.material.color = colour;
    }
}

The call it from any other script:

public class AnyOtherScript: MonoBehaviour {
    
    MaterialColourChanger colourChanger;
    
    void Start() {
        colourChanger = GameObject.Find("TheColourChangerGameObject").GetComponent<MaterialColourChanger>();
    
        // Get the renderer component for the object that this script is attached to...
        Renderer thisObjectsRenderer = GetComponent<Renderer>();
        Color myColour = Color.red;
        
        // Pass it all to the colour changer script to change the colour
        colourChanger.ChangeMaterialColour(thisObjectsRenderer , myColour);
    }
}

If you are sure of the correct name of the property shader, it's because random numbers are float's between 0, 1 , not up to 255. fix it:

[SerializeField] private Material myMaterial;
void Start()
{
    var red = Random.Range(180, 231)/255f;
    var green = Random.Range(200, 255)/255f;
    var blue = Random.Range(50, 100)/255f;

    var newColor = new Color(red,green,blue);
    myMaterial.SetColor("_SkinColor",newColor);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM