Material Change Problems Unity 3D. Cant see any errors

耗尽温柔 提交于 2019-12-13 05:01:24

问题


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.


回答1:


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;
}



回答2:


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




回答3:


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;
}


来源:https://stackoverflow.com/questions/23958915/material-change-problems-unity-3d-cant-see-any-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!