- Home /
Problems with if statements
Hey people, trying to get this code to work but i can't figure out why it shouldn't work.
I'm trying to have a button that you click that will make a text box that you can enter your name "textFieldString" and when you push enter the game will start the game, loading the first level.
var normalText : Texture2D;//normal texture
var hoverText : Texture2D;//Mouse enter texture
var audio1 : AudioSource; //attached mouse over audio
var audio2 : AudioSource; //attached mouse down audio
public var scoreKeeperPrefab:GameObject;
var click : boolean = false;
var textFieldString = "Player 1";
private var scoreKeeperRef:GameObject;
private var scoreKeeperScriptRef:ScoreKeeper;
function Awake(){
if(GameObject.Find("ScoreKeeper") == null){
Instantiate(scoreKeeperPrefab).name = "ScoreKeeper";
}
scoreKeeperRef = GameObject.Find("ScoreKeeper");
scoreKeeperScriptRef = scoreKeeperRef.GetComponent(ScoreKeeper);
}
function OnMouseEnter () {
audio1.Play();//plays mouse over audio
guiTexture.texture = hoverText;
}//changes texture when mouse goes over button
function OnMouseExit () {
guiTexture.texture = normalText;
}//changes it back to origin texture when mouse exits button
function OnMouseDown(){
audio2.Play();
scoreKeeperScriptRef.SetPlayerName(textFieldString);
click = true;
} //loads level and plays clicking audio
function OnGUI (){
if(click ==true){
textFieldString = GUI.TextField (Rect (250, 250, 100, 40), textFieldString);//opens player name if button is clicked
}
}
function Update (){
if(click ==true){
if(Input.GetButton("Enter")) {// if click = true and then enter is pushed start game.
Application.LoadLevel("level 1");
}
}
}
Any Help would be greatly Appreciated!
you said a double negative in your first sentence "I can't figure out why it shouldn't work" which means it does work and you can't find a reason for it not to work. By the way I will answer your question in a second. :)
Answer by TheVectorHunter · Aug 13, 2012 at 05:24 PM
I think your problem was this:
Input.GetButton is repeating:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButton.html
What you want to put in is:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetKeyDown.html
Make sure that the level you are loading is added into the index in the build menu, and it has the exact name.
If my first answer did not work for you then that means that input from the keyboard is being captured by your textfield. Which means you need to either be able to click out of the textfield using your mouse so it is inactive or you need to disable input into it period once the player presses enter.
Answer by tw1st3d · Aug 13, 2012 at 08:43 PM
var normalText : Texture2D;//normal texture
var hoverText : Texture2D;//Mouse enter texture
var audio1 : AudioSource; //attached mouse over audio
var audio2 : AudioSource; //attached mouse down audio
public var scoreKeeperPrefab:GameObject;
var click : boolean = false;
var textFieldString = "Player 1";
private var scoreKeeperRef:GameObject;
private var scoreKeeperScriptRef:ScoreKeeper;
function Awake(){
scoreKeeperRef = GameObject.Find("ScoreKeeper");
scoreKeeperScriptRef = scoreKeeperRef.GetComponent(ScoreKeeper);
//You were casting a conditional before finding the GO
if(GameObject.Find("ScoreKeeper") == null){
Instantiate(scoreKeeperPrefab).name = "ScoreKeeper";
}
}
function OnMouseEnter () {
audio1.Play();//plays mouse over audio
guiTexture.texture = hoverText;
}//changes texture when mouse goes over button
function OnMouseExit () {
guiTexture.texture = normalText;
}//changes it back to origin texture when mouse exits button
function OnMouseDown(){
audio2.Play();
scoreKeeperScriptRef.SetPlayerName(textFieldString);
click = true;
} //loads level and plays clicking audio
function OnGUI (){
if(click == true){
textFieldString = GUI.TextField (Rect (250, 250, 100, 40), textFieldString);//opens player name if button is clicked
}
}
function Update (){
if(click ==true){
if(Input.GetButton("Enter")) {// if click = true and then enter is pushed start game.
Application.LoadLevel("level 1");
}
}
}
why did you answer the question with the same code?
His problem was not Input.GetButton That doesn't repeat. It's a check to see if a button was pushed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075622im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
if else statement doesnt work 1 Answer
Check renderer.enabled statement from another object 3 Answers
How do you say in between for an if statement? 2 Answers
some scripting help 2 Answers
Destorying despite IF Statement 1 Answer