So I’m making some kind of Minecraft command block. I really need help with the strings.
Heres my code:
var stringToEdit : String = “/type in here”;
var clicked : boolean = false;
var killCommand = “/kill”;
var testCommand = “/test”;
var btnTexture : Texture;
function Update()
{
if(killCommand == "/kill")
{
print("Profanity Found");
}
}
//function Update(){
//CheckCode
//if(killCommand == "/kill" || killCommand == "/Kill")
//{
//Debug.Log("Killed the player");
//Application.LoadLevel(Application.loadedLevel);
//}
//}
function OnMouseDown(){
if(Input.GetMouseButtonDown (0))
{
clicked = true;
}
}
function OnGUI () {
if(clicked == true)
{
// Make a text field that modifies stringToEdit.
stringToEdit = GUI.TextField (Rect (0, 0, 200, 20), stringToEdit, 35);
}
}
But for some reason I can’t get this to work. The part I need help with is:
function Update()
{
if(killCommand == “/kill”)
{
print(“Profanity Found”);
}
}
I’m going to have multible commands like this. Please Help!!
The main problem in your script seems to be that you never assign the text from the text field to a variable that gets checked for its content. You need to get what the player has typed in and then check if it was a command that your game recognizes.
Here is a modified version that should do approximately what you want.
#pragma strict
//The string that is edited in the text field
public var stringToEdit : String;
//The command as entered by the player
public var command : String;
//Boolean to track when the player clicks to enable the text field
public var clicked : boolean;
//Boolean to track when the player has confirmed the command to enter
public var shouldCheckCommand : boolean;
public var btnTexture : Texture;
function Start()
{
//Initialize variables
stringToEdit = "/type in here";
clicked = false;
shouldCheckCommand = false;
}
function Update()
{
//Check for the mouse click - the text field is invisible
if(Input.GetMouseButtonDown (0) && clicked == false)
{
//Reset the string to default
stringToEdit = "/type in here";
clicked = true;
}
//Check for the Return/Enter key - the text field is visible
else if (Input.GetKeyDown(KeyCode.Return) && clicked == true)
{
command = stringToEdit; //Here is the key part - the string from the text field is assigned to the command variable
clicked = false;
shouldCheckCommand = true;
}
//Check if the player has confirmed the command entered
if (shouldCheckCommand == true)
{
CheckCommand();
}
}
function CheckCommand()
{
//Check which command was entered and take the appropriate action
if (command == "/kill" || command == "/Kill")
{
Debug.Log("Kill the player");
}
shouldCheckCommand = false;
}
function OnGUI()
{
if(clicked == true)
{
stringToEdit = GUI.TextField (Rect (0, 0, 200, 20), stringToEdit, 35);
}
}
This script activates the text field on a mouse click. Once the command is entered, clicking outside the text field to deselect it and hitting the Return key will “enter” the command.