【发布时间】:2020-04-27 17:39:57
【问题描述】:
我正在制作一个小型平台游戏,在其中,我制作了一个场景,您可以在其中更改播放器的颜色。我制作了 3 个按钮,当您单击它们时它们会改变颜色。我还制作了两个代码文件,但它们不起作用。我在控制台中也看不到任何错误。 P.S 变色代码和按钮与游戏对象位于不同的场景。
这是改变颜色的按钮的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourManager : MonoBehaviour
{
public static int colour;
public void DefaultBlue()
{
colour = 0;
}
public void Green()
{
colour = 1;
}
public void Red()
{
colour = 2;
}
}
这是游戏对象本身的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ColourTarget : MonoBehaviour
{
void Start()
{
rend = GetComponent<Renderer>();
rend.sharedMaterial = materials[0];
rend.enabled = true;
}
private Renderer rend;
public int lcolour = ColourManager.colour;
public Material[] materials;
private void Update()
{
if (lcolour == 0)
{
rend.sharedMaterial = materials[0];
}
if (lcolour == 1)
{
rend.sharedMaterial = materials[1];
}
if (lcolour == 2)
{
rend.sharedMaterial = materials[2];
}
}
}
【问题讨论】: