C# 按键时在两种颜色之间切换

C# 按键时在两种颜色之间切换,c#,unity3d,colors,C#,Unity3d,Colors,我目前正在学习C#和Unity,我创建了一个简单的游戏,你可以通过点击“C”键(将变为绿色)来改变玩家的颜色(默认为红色)。 代码可以工作,但问题是,我不知道如何创建一个代码,通过使用相同的键(“C”)将绿色再次更改为红色。 我知道的唯一选择是创建另一个if,但使用不同的按钮。 这是我的密码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class cubecolor

我目前正在学习
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;

    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类立方体颜色:单行为{
//用于初始化
无效开始(){
gameObject.GetComponent().material.color=color.red;
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.C))
gameObject.GetComponent().material.color=color.green;
}
}

if(Input.GetKey(KeyCode.C))
中创建一个if-else结构:

void更新(){
if(Input.GetKey(KeyCode.C))
{
var myObject=gameObject.GetComponent();
颜色currentColor=myObject.material.Color;
如果(currentColor==Color.green)
{
myObject.material.color=颜色.红色;
}
其他的
{
myObject.material.color=颜色.绿色;
}
}

您需要按C键将颜色更改为红色,然后再次按C键将颜色更改为绿色。这是非常基本的编程内容

你需要使用一个布尔变量,然后每次按键时将其翻转或反转。翻转是通过使用
符号完成的。如果布尔变量为真,则使用红色,否则使用绿色。我鼓励你阅读和理解C#中的。你需要这些符号才能自己制作一个实际的游戏。

Renderer yourRenderer;
bool red = true;

// Use this for initialization
void Start()
{
    yourRenderer = gameObject.GetComponent<Renderer>();
    //Use red as default
    yourRenderer.material.color = Color.red;
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.C))
    {
        //Flip the boolean variable to the opposite of what it is
        red = !red;

        //If true, use red color
        if (red)
        {
            yourRenderer.material.color = Color.red;
        }
        //If false, use green color
        else
        {
            yourRenderer.material.color = Color.green;
        }
    }
}
渲染器;
布尔红=真;
//用于初始化
void Start()
{
yourRenderer=gameObject.GetComponent();
//使用红色作为默认值
yourRenderer.material.color=color.red;
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.C))
{
//将布尔变量翻转到与其相反的位置
红色=!红色;
//如果为true,则使用红色
如果(红色)
{
yourRenderer.material.color=color.red;
}
//如果为false,则使用绿色
其他的
{
yourRenderer.material.color=color.green;
}
}
}

与此无关,请使用
GetKeyDown
而不是
GetKey
,否则在一帧中多次更改颜色时会遇到问题,因此无法看到颜色的变化。

我会这样做

Private Renderer objectRenderer = gameObject.getComponent<Renderer>();
Private Update() {
    If(Input.GetKeyDown(KeyCode.C))
        If(objectRenderer.material.color == Color.green)    
           objectRenderer.material.color = Color.red;
        else
           objectRenderer.material.color = Color.green;
}
Private Renderer objectRenderer=gameObject.getComponent();
私有更新(){
If(Input.GetKeyDown(KeyCode.C))
If(objectRenderer.material.color==color.green)
objectRenderer.material.color=color.red;
其他的
objectRenderer.material.color=color.green;
}

使用
bool
变量即可轻松完成:

            using System.Collections;
            using System.Collections.Generic;
            using UnityEngine;

            public class cubecolor : MonoBehaviour {
                private bool isGreen = false; /*Let's create a boolean to store if the cube  
                is green or red*/


                // 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){
                        if(isGreen){ //Check if the color is green
                            gameObject.GetComponent<Renderer> ().material.color = Color.red;
                        }else{
                            gameObject.GetComponent<Renderer> ().material.color = Color.green;
                        }
                        isGreen = !isGreen; //Switch the boolean.
                    }
            }
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类立方体颜色:单行为{
private bool isGreen=false;/*如果多维数据集
是绿色还是红色*/
//用于初始化
无效开始(){
gameObject.GetComponent().material.color=color.red;
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.C){
如果(绿色){//检查颜色是否为绿色
gameObject.GetComponent().material.color=color.red;
}否则{
gameObject.GetComponent().material.color=color.green;
}
isGreen=!isGreen;//切换布尔值。
}
}

好的,那么将颜色更改为绿色的
更新方法就正确了..?为什么不创建一个按钮,让它做同样的事情或调用更新方法..?是什么阻止了你这么做..?对不起,我不太明白你的意思。如果你不介意的话,你能用其他话解释一下吗?检索ga是无效的meObject.getComponent()两次!是的,我的坏,修复了它^^AFAIK,在声明class属性时无法检索组件。你必须在
Awake
/
Start
方法中执行此操作。它正在工作!非常感谢!不客气。记住,这与工作与否无关。重要的是理解我写的内容,以便你可以应用它来解决你的问题您未来代码中的缺陷。愉快的编码!是的,您完全正确!再次感谢!
            using System.Collections;
            using System.Collections.Generic;
            using UnityEngine;

            public class cubecolor : MonoBehaviour {
                private bool isGreen = false; /*Let's create a boolean to store if the cube  
                is green or red*/


                // 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){
                        if(isGreen){ //Check if the color is green
                            gameObject.GetComponent<Renderer> ().material.color = Color.red;
                        }else{
                            gameObject.GetComponent<Renderer> ().material.color = Color.green;
                        }
                        isGreen = !isGreen; //Switch the boolean.
                    }
            }