重大变更问题Unity 3D.无法看到任何错误 [英] Material Change Problems Unity 3D. Cant see any errors

查看:123
本文介绍了重大变更问题Unity 3D.无法看到任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在C#中使用Unity3D,并且正在尝试一个简单的脚本来更改对象的材料.初始材料集有效,但是单击它的更改无济于事. 我知道这是基本知识,这里已经有很多关于此主题的问题,但是我所看到的所有问题,我都曾尝试过并且没有奏效. 该程序不会抛出任何错误,材质也不会改变.我尝试使用纹理代替,甚至尝试了renderer.material.color,只是想看看我的文件是否有问题.在if语句中,我尝试过==和=.

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.

这是我的代码:

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 :)

更新

我添加了很多debug.logs,看来颜色先变,然后再变回来.你知道我怎么能阻止这种情况发生?

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?

这是一个以灰色开头并单击的gif. http://gyazo.com/7c556ea79bb9b7277ae23d14ee44f8d2

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

这是一个以黑色开头并单击的gif. http://gyazo.com/c6e00d858ab8d47436e689dd124bd762

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

进一步更新

我尝试添加第三种材料并尝试使用if语句的顺序,即使没有使用这些材料,该程序也将遍历所有这些材料.并且一旦被单击的材料将始终成为最后一个if语句中的材料.

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.

推荐答案

您更新的代码正在使用赋值而不是进行等效性测试.

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

应该是:

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

这篇关于重大变更问题Unity 3D.无法看到任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆