0
votes

I'm wanting to have two textures switch when ever I press the space bar in my project. But when I do, nothing is happening.

can anyone see what I'm doing wrong with my logic? I had this working with switching between 2 shaders, but not it is materials, it isn't working. I'm very confused by this.

void Switch()
{
    Debug.Log(gameObject.renderer.material.name);

    if(Input.GetButtonDown("Jump"))
    {
        diffuse = false;
        //if(gameObject.renderer.material == diffuse_Material)
    }
    else 
    {

        diffuse = true;
    }


    if(diffuse == true)
    { 
        
        gameObject.renderer.material = mask_Material;   
        print("1");
        Debug.Log(gameObject.renderer.material.name);
    }
    else
    {

        gameObject.renderer.material = diffuse_Material;
        print("2");
        Debug.Log(gameObject.renderer.material.name);
    }

My codes changes the material once, but doesn't change back again when I press the space bar again.

update

I've taken my code out of the update method and placed it in its own one. I've since modified it to be a "simple" bool check. Now when I play the game my object starts with the material I first want on it, but when I hit space, it only changed the material for a fraction of a second.

How can I do it so that on the press of a single button, my bool toggles between true and false. I thought i had it, but I guess I don't. My debug log now looks like this:

DepthMask (instance)

1

DepthMask (instance)

2

Diffuse (instance)

Diffuce (instance)

DepthMask (instance)

DepthMask (instance)

1

2
It's just a guess because I don't know Unity so I'm not going to post it as an answer, but try adding diffuse=false; in the if statement? - Bart Beyers
That variable only set the colour variable of the diffuse texture mate. Also, which if statement? At the moment, my print value of 1 doesn't even print either. - N0xus
I was thinking the part where you say print("1"); yes because atm it looks like it gets set to true but never to false again. But if it never prints 1 then it's going to be something else. As I said, it was just a guess :) - Bart Beyers
Thanks man, I've modified the code to try something "simpler" - N0xus
Input.GetButtonDown("Jump") is fired only once per click. It will toggle your object material just in one Frame, so you wont be able to see changes with your eyes. ` diffuse` will always be true. - Brivvirs

2 Answers

0
votes

I think the problem is the following:

When the user presses the jump key the boolean gets set to true, the material changes and its all good. BUT then the update method gets called again, and followed by that I'm assuming your Switch method also. Input.GetButtonDown("Jump") will now evaluate to false, causing your boolean value diffuse to become true again, and the material changes back.

Is the material supposed to toggle everytime the users presses the Jump button? If so, change your code to (the else statement can be completely removed):

void Switch()
{
    Debug.Log(gameObject.renderer.material.name);
    if(Input.GetButtonDown("Jump"))
    {
        diffuse = !diffuse;
        // material only needs to change if diffuse changed
        if(diffuse)
        { 
            gameObject.renderer.material = mask_Material;   
            print("1");
            Debug.Log(gameObject.renderer.material.name);
        }
        else
        {
            gameObject.renderer.material = diffuse_Material;
            print("2");
            Debug.Log(gameObject.renderer.material.name);
       }
    }
}

EDIT: included the entire Switch method.

0
votes

You could try to compare

renderer.sharedMaterial

instead of

renderer.material

to get the non-instanced version of the material. Something like this:

public Material a;
public Material b;

void Update () 
{
    if (Input.GetButtonDown("Jump"))
    {
        if (renderer.sharedMaterial == a)
        {
            gameObject.renderer.material = b;
        }
        else
        {
            gameObject.renderer.material = a;
        }
    }
}