我正在学习C#,Unity我创建了一个简单的游戏,您可以通过单击"C"键(将变为绿色)更改播放器的颜色(默认情况下为红色).代码可以工作,但问题是,我不知道如何创建一个代码,通过使用相同的键("C")再次将绿色更改为红色.我知道的唯一选择是创建另一个if但使用不同的按钮.这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubecolor : MonoBehaviour {
// Use this for initialization
void Start () {
gameObject.GetComponent<Renderer> ().material.color = Color.red;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.C))
gameObject.GetComponent<Renderer> ().material.color = Color.green;
}
}
Run Code Online (Sandbox Code Playgroud)