Andre
Andre

Reputation: 49

How to change texture of game object in Unity?

I'm working on Unity for a School Project and I am using Kinect to recognize facial expressions. The "default" expression it shows is Neutral and it changes as I change facial expression to Happy or Suprised, etc.

I created a game object called Face where I set the different textures (faces that the PC shows in response to my expression) and I wanted him to change when I change my facial expression. But for some reason it is not working.

I am using C# and first I set this:

public static Texture[] textures = new Texture[7];
public Texture neutral, smiling, happy, angry, sad, kissing, surprised;
public GameObject Face;
public Renderer rend;

In the Start I have this:

    Face = GameObject.Find ("Face");

    textures[0] = neutral;
    textures[1] = smiling;
    textures[2] = happy;
    textures[3] = angry;
    textures[4] = sad;
    textures[5] = sad;
    textures[6] = surprised;

By the way, it didn't find Face so I put it there by the inspector and I did the same with the textures/faces. Then in the Update I put this which is defined below:

    ClassifyAndApply(numbers);


private void SaveAnimUnits()
    {
        numbers[0] = _animUnits.LipRaiser;
        numbers[1] = _animUnits.JawLowerer;
        numbers[2] = _animUnits.LipStretcher;
        numbers[3] = _animUnits.BrowLowerer;
        numbers[4] = _animUnits.LipCornerDepressor;
        numbers[5] = _animUnits.OuterBrowRaiser;
    }

    private void ClassifyAndApply(float[] units){

//      Renderer rend = GetComponent<Renderer>();
//      Face = GameObject.Find ("Face");



        if (units[2] <= 0.264888){
            if (units[3] <= 0.817408){
                if (units[1] <= 0.181886){
                    if (units[0] <= -0.216908){
                        if (units[4] <= 0.395523){
                            if (units[1] <= 0.104226){ 
                                Face. GetComponent<Renderer>().material.mainTexture = textures[3];
                            }
                            else{ 
                                Face. GetComponent<Renderer>().material.mainTexture = textures[0];
                            }

This tree continues but I think my problem is this but I am not sure.

Face. GetComponent<Renderer>().material.mainTexture = textures[0];

Upvotes: 1

Views: 10075

Answers (1)

izeed
izeed

Reputation: 1731

Try executing Face.GetComponent<Renderer>().material.mainTexture = textures[0]; without the huge if structure, to be 100% sure it is not working. Are you sure you have all the textures drag-n-dropped in the inspector ?

If that is the case then try creating new material (right-mouse-in-project-tab -> Create -> Material) and give it the Standard shader. Then give the material to your Face object.

Setting mainTexture only works if your object is using material with shader that has a parameter named "MainTexture" (some simple color or exotic fx shaders might not have use for texture and and have no such parameter)

Upvotes: 3

Related Questions