Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chippers · Aug 13, 2012 at 04:45 PM · loadleveliftextfieldstatement

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loius · Aug 13, 2012 at 05:18 PM 0
Share

What's not working?

avatar image TheVectorHunter · Aug 13, 2012 at 05:20 PM 0
Share

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. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheVectorHunter · Aug 13, 2012 at 08:33 PM 0
Share

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.

avatar image Chippers · Aug 14, 2012 at 01:21 PM 0
Share

Thank you this fixed my problem!

avatar image TheVectorHunter · Aug 14, 2012 at 01:46 PM 0
Share

can you vote up my answer please too?

avatar image tw1st3d · Aug 14, 2012 at 04:24 PM 0
Share

I can't believe I didn't see that! +'d this.

avatar image
0

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");
  }
  }
 } 
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheVectorHunter · Aug 13, 2012 at 08:59 PM 0
Share

why did you answer the question with the same code?

avatar image tw1st3d · Aug 13, 2012 at 09:05 PM 0
Share

I didn't. I fixed your problem.

avatar image TheVectorHunter · Aug 13, 2012 at 09:05 PM 0
Share

I don't have a problem.

avatar image TheVectorHunter · Aug 13, 2012 at 09:06 PM 0
Share

And his problem is not fixed in your code.

avatar image tw1st3d · Aug 13, 2012 at 09:16 PM 0
Share

His problem was not Input.GetButton That doesn't repeat. It's a check to see if a button was pushed.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges