Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by Proportion 1 · Mar 13, 2010 at 08:30 AM · gui

GUI Text Enter Name

Hi i am a total noob when it comes to scripting so i thought i would get my head around the gui text to start with.

i have this javascript attached to a camera.

var stringToEdit = "Player1";

function OnGUI () { GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");

stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);

}

what i would like to do is have the player enter his name then when the player pressed the return button it would take out the text field and change the GUI.Label to say "hello " stringToEdit " how are you." or something along those lines.

Please dumb it down as much as possible for me as im no good at scripting thanks!!

Comment
Add comment
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

3 Replies

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

Answer by qJake · Mar 13, 2010 at 08:53 AM

Well, I'm not going to write the script out for you, but I can explain what you might want to try.

You would basically check to see if the user pressed enter using if(Input.GetKeyDown("enter")), and when that happened, set some sort of flag (just a true/false that you keep at the very top of your script, outside of any functions). In your GUI function, you would check that flag. If it's false, display the text box. If it's true, display the label with the name in it. Something like this:


if(userHasPressedEnter)
{
     // Draw "Hello" text here
}
else
{
     // Draw GUI.TextField here
}
Comment
Add comment · Show 1 · 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 Proportion 1 · Mar 13, 2010 at 10:18 AM 0
Share

hey thanks, i did some nutting out with the way you said and this was my final script for anyone that cares.

var stringToEdit = "Player1"; var userHasHitOk : boolean = false;

function Update() { if(Input.Get$$anonymous$$eyDown("enter")) {
userHasHitOk = true; }

}

function OnGUI () {

 if (userHasHitOk)
 {
 
     GUI.Label (Rect (10, 10, 100, 30), "Hello " + stringToEdit);
 }
 else
 {
 GUI.Label (Rect (10, 10, 100, 30), "Enter Name:");

 stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
 }

}

avatar image
0

Answer by Bobby · Sep 24, 2010 at 05:59 PM

The Final script worked well, how can you then pass that name (stringToEdit) to another Scene.

For Example Alex collected all the Gems

Comment
Add comment · 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
0

Answer by Charles-Van-Norman · Feb 10, 2011 at 12:34 AM

To save something to another scene, use DontDestroyOnLoad. To save something even after the application quits, use PlayerPrefs.Set("name","Jacob") and PlayerPrefs.Get("name")

To make a text field go away when you press Enter, use:

[code]

using UnityEngine; using System.Collections;

public class example : MonoBehaviour {

bool userHasHitReturn = false; string stringToEdit = "";

void OnGUI() { Event e = Event.current; if (e.keyCode == KeyCode.Return) userHasHitReturn = true; else if (false == userHasHitReturn) stringToEdit = GUI.TextField(new Rect(0,0,100,50), stringToEdit, 25); } } [/code]

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Enum option into a game menu gui 1 Answer

I am trying to send a 2D texture to my first person controller to display on the GUI 1 Answer

Adding GUI Skin to a script in the project library 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