Easy, beginner scripting question (C#).

Hey guys,

I’m currently running through the Unity scripting tutorials and challenging myself to make a game with a win state at the end of each tutorial.
On Tutorial 1, I want the player to “win” when they press ‘R’ ‘G’ and ‘B’ in succession. After they press the correct buttons, key input is disabled and a GUI message appears that says “You win!”
Could someone modify the script to show me how to do this?

This is my current script:

using UnityEngine;
using System.Collections;

public class change_color : MonoBehaviour 
{

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () 
	{
		if(Input.GetKeyDown(KeyCode.R))
		{
			gameObject.renderer.material.color = Color.red;
		}
		if(Input.GetKeyDown(KeyCode.G))
		{
			gameObject.renderer.material.color = Color.green;
		}
		if(Input.GetKeyDown(KeyCode.B))
		{
			gameObject.renderer.material.color = Color.blue;
		}
	}
}

Fixing my post, you dont have to add material, leave the script as it is, just add a new material to your object instead of that gray default one .

I can currently turn the cube either red, green or blue. Attaching a material is not the issue. I am modifying my above post for new rules that might be easier to script.

I think you should consider using booleans for something like this. Just set 3 booleans that check whether the player has pressed R,G or B.

When all the booleans are true then display a message saying “You Win!”

I think that’s the best idea. Could you provide an example?

I use JS, hope that won’t be a problem :

var HitR : boolean = false;
var HitG : boolean = false;
var HitB : boolean = false;


function Update(){

if(Input.GetKeyDown(KeyCode.R)){
HitR = true;
gameObject.renderer.material.color = Color.red;
}
if(Input.GetKeyDown(KeyCode.G)){
gameObject.renderer.material.color = Color.green;
HitG = true;
}
if(Input.GetKeyDown(KeyCode.B)){
gameObject.renderer.material.color = Color.blue;
HitB = true;
}
}


function OnGUI(){

if(HitR  HitG  HitB){
GUI.Label(Rect(Screen.width/2 - 50,Screen.height/2 - 50, 100,50),"You Win");
}
}

Wow, thanks! I will translate this to C# and then update the thread.

Thanks, Intense_Gamer. It worked great. Here is the code in C#:

using UnityEngine;

using System.Collections;

public class change_color : MonoBehaviour
{
bool HitR=false;
bool HitG=false;
bool HitB=false;


    // Use this for initialization
    void Start () {
    }
    // Update is called once per frame
    void Update () 
    {
        if(Input.GetKeyDown(KeyCode.R))
        {
			HitR = true;
            gameObject.renderer.material.color = Color.red;
        }
        if(Input.GetKeyDown(KeyCode.G))
        {
			HitG = true;
            gameObject.renderer.material.color = Color.green;
        }
        if(Input.GetKeyDown(KeyCode.B))
        {
			HitB = true;
            gameObject.renderer.material.color = Color.blue;
        }
    }
void OnGUI ()
	{
	if(HitR  HitG  HitB)
		{
			GUI.Label(new Rect(Screen.width/2 - 50, Screen.height/2 - 50, 100, 50), "You Win");
		}
	}
}

Glad I could help.

Also as a future reference, I think it’s best to delete any unused unity functions. So void Start() or Update() or OnGUI() or any other of the like. Not that important, just keeps the script clean and I think saves some frames performance wise.

Noted. Thanks tom1499!