McGlovin
McGlovin

Reputation: 43

Material Change Problems Unity 3D. Cant see any errors

I am using Unity3D in C# and am trying a simple script to change the material of an object. The initial material set works, but the change on the click of it does not do anything. I know this is basic, and there are lots of questions on this topic here already, but all of them that i have seen, i have tried and not worked. The program does not throw back any errors, the material simply does not change. i tried it with a texture instead and even a renderer.material.color at one point just to see if my files had problems. On the if statements i have tried == and =.

please help me find what I've done wrong, even if its a small typo i cant seem to see one.

here is my code:

using UnityEngine;
using System.Collections;

public class tilechange : MonoBehaviour {

public Material Grey;
public Material Black;

void Start()
{
    renderer.material = Grey;
}


void OnMouseDown () {
    if (renderer.material = Grey)
    {
        renderer.material = Black;
    }
    if (renderer.material = Black)
    {
        renderer.material = Grey;
    }
}
}

Any help would be appreciated :)


Update

I have added lots of debug.logs and it appears that the colour changes, then changes back. do you know how i could stop this happening?

here is a gif starting on grey and clicking. http://gyazo.com/7c556ea79bb9b7277ae23d14ee44f8d2

here is a gif starting on black and clicking. http://gyazo.com/c6e00d858ab8d47436e689dd124bd762

they both act differently, which seems like it could be something to do with the problem


Further Update

I tried adding in a third material and experimenting with the order of the if statements, the program runs through all of them, even if the materials are not used. and the material once clicked on always becomes that of the one in the last if statement.

Upvotes: 0

Views: 1531

Answers (3)

ChrisW
ChrisW

Reputation: 1

I know this is old but I'm answering for the sake of anyone landing here. The problem is the two if statements. They both get executed so they are canceling each other out. You need to use an "else if" for the second one so it only gets executed if the first one isn't. This should accomplish what you are wanting, switching the material.

if (renderer.material == Grey) {
    renderer.material = Black;
} else if (renderer.material == Black) {
    renderer.material = Grey;
}

Upvotes: 0

CiscoIPPhone
CiscoIPPhone

Reputation: 9477

Your updated code is using assignment instead of testing for equivalence.

if (renderer.material = Grey)
{
    renderer.material = Black;
}
if (renderer.material = Black)
{
    renderer.material = Grey;
}

Should be:

if (renderer.material == Grey)
{
    renderer.material = Black;
}
if (renderer.material == Black)
{
    renderer.material = Grey;
}

Upvotes: 1

Nathan
Nathan

Reputation: 51

I don't think you've assigned the material color correctly, since you've created materials, but haven't set any properties on those materials.

See Unity documentation for SetColor

(Edit: Make sure to select 'C#' from the top right)

(Edit2) Based on your updated question, I believe it's changing -> changing back because it's registering multiple mousedown events. Try adding a tick to delay as a minimum before changing the color back

See the Unity topic that is related

Upvotes: 0

Related Questions