Help with making a triggered gui message

alright so this is like the fourth time i have asked for help on this same project and i apologize to everyone for being so bad at coding. it has never been my strong point, the thought process for coding just is not how my brain is wired. all im trying to do here is simply make it so that when a player gets the password wrong in my keypad script a message pops up on the screen for like 3 or so second telling them they got it wrong and then disappears, but so far i cant even get my message to display. i would greatly appreciate any help as always. my code is listed below.

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



public class KeyPad: MonoBehaviour {
	//public variables
	public Texture2D icon;
	public string SetPassword = "12345";
	public string PlayerPassword = "";
	public string message = "Incorrect Password please try again";
	public bool KeyPadEnabled = false;
	public bool DoorIsOpen = false;
	public Transform target; 
	
	public MouseLook Pc;
	public MouseLook Mc;
 	

	
	//trigger used to enable gui elements when player enters trigger zone
	void OnTriggerEnter (Collider other) {
		
		if(other.gameObject.tag.Equals ("Player"))
		{
			Pc.enabled=false;
			Mc.enabled=false;
			KeyPadEnabled = true;
		}
	
	}
	
	//trigger used to disable gui elements when player enters trigger zone
	void OnTriggerExit (Collider other) {
		
		if(other.gameObject.tag.Equals("Player"))
		{
			Pc.enabled=true;
			Mc.enabled=true;
			
			KeyPadEnabled = false;
			PlayerPassword = "";
		
			if( DoorIsOpen == true)
			{
				target.Translate (Vector3.back *250* Time.deltaTime);
				DoorIsOpen = false;
			}
		}
		
	}
		
	//gui function
	void OnGUI () {
		
		//check if gui elemnts should be shown.
		if(KeyPadEnabled == true)
		{
			//display gui elemnts
			GUI.Box (new Rect (10,10,300,300), new GUIContent(icon));
			PlayerPassword = GUI.PasswordField (new Rect(60, 60, 200, 35), PlayerPassword, "*"[0],45);
			
			//check for player input
			if(Input.GetKeyUp(KeyCode.KeypadEnter))
			{  
				//check passwords
				if(PlayerPassword.Equals (SetPassword))
				{
				//translate targer and set control variable to true
				target.Translate (Vector3.forward *150* Time.deltaTime);
				DoorIsOpen = true;
				}
				else
				{
					GUI.Label( new Rect(100, 150, 100, 35), message);
					PlayerPassword = "";
				}
				
			}
		
		}
	}
			
	

}

1 Answer

1

Not sure if this is still open or not, but instead of using

if(PlayerPassword.Equals (SetPassword))

try using

PlayerPassword == SetPassword

The equals function compares a string to an object, rather than string to string.
Also what you need to do to display the message for three seconds is start a timer in the else statement

// declared as a class member
float displayMessage = 3.0f

// else statement updated
else
{
     displayMessage -= Time.deltaTime;
     if (displayMessage > 0)
     {
          GUI.label(new Rect(100, 150, 100, 35), message);
          PlayerPassword = "";
          displayMessage = 3.0f;
     }
}