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 LucasMars · Aug 01, 2013 at 08:55 PM · variablevaluevaluesgetset

Get Value/String From GUIText

This is a really simple question but I cannot find an answer. I want to get a int value from a GUIText object and add a number onto it. There are three scripts all modifying a value so I want to get the current set value of the GUIText and then set the value to a variable. Here is some code...

 var GUITextVar : GUIText;
 var intvar : int = 0;
 var currentval : int;
 var equal : int;
 
 function Update(){
     intvar+=1;
     //Some Code to get the current value of the GUIText
     equal=currentval+intvar;
     GUITextVar.text=equal;
 }

The Code isn't perfect as I just made it up. It would work otherwise.

Thanks

[EDIT]The values being edited from the other scripts are not static [/EDIT]

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
0
Best Answer

Answer by LucasMars · Aug 01, 2013 at 09:25 PM

Here is the finished code...

 var intVar : int= 0;
 var equal : int;
 var intValueString;
 var GUITextvar : GUIText;
 var currentVal : int;
 var guiTextString = "0";
 
 function OnMouseOver(){
     var score = 0;
     
     if(Input.GetMouseButtonDown(0)){
         intVar+=5;
         currentVal += intVar;
         guiTextString=GUITextvar.text;
         equal=currentVal+ int.Parse(guiTextString);
         GUITextvar.text=" "+equal;
     }
 }
 
 function Update(){
     var currentVal : int;
 }

Thanks to Jamora and RyanZimmerman87 for providing great answers and both answers have been voted up (by me, :D)

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 RyanZimmerman87 · Aug 01, 2013 at 09:27 PM 0
Share

Nice that looks good!

avatar image
1

Answer by RyanZimmerman87 · Aug 01, 2013 at 09:00 PM

I haven't used GUIText for a long time so I'm not really 100% sure on this. But I would create a public static string to store the value since it is shared by multiple scripts.

 int intVar = 0;
 int currentVal;
 int equal;
 
 string intValueString;
 
 void Update()
 {
 ++intVar;
 
 equal = currentVal + intVar;
 
 intValueString = "" + equal;
 }

Then use the intValueString for your text, but that's the part I'm not 100% on because I don't think I've used GUIText lately, should be quite straightforward though I would imagine.

Edit:

If you don't need to use the GUItext object you could just use OnGUI()

example:

 public GUIStyle GUIStyleTest;
 
 void OnGUI()
 {
 
 GUI.Label (new Rect (640 ,0, 100, 100), intValueString, GUIStyleTest);
 
 }
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 LucasMars · Aug 01, 2013 at 09:10 PM 0
Share

Thanks for the answer. I was using Unity-Script (I think that's JavaScript) so I had to convert it. Here is the code now...

 var intVar : int= 0;
 var equal : int;
 var intValueString;
 var GUITextvar : GUIText;
 var currentVal : int;
 
 function On$$anonymous$$ouseOver(){
     var score = 0;
     
     if(Input.Get$$anonymous$$ouseButtonDown(0)){
         intVar+=5;
         equal = currentVal + intVar;
         intValueString = equal.ToString();
         GUITextvar.text = intValueString;
     }
 }
 
 function Update(){
     var currentVal : int;
 }

The value is not static as it changes it in certain frames.

What should I do for that?

[EDIT] I've voted on your answer[/EDIT]

avatar image Jamora · Aug 01, 2013 at 09:17 PM 0
Share

If it's the right answer the you should select it as the correct answer by pressing the tick mark.

avatar image LucasMars · Aug 01, 2013 at 09:18 PM 0
Share

I am not getting the required function from the answers but I think I know what to do now with both the answers :D

avatar image RyanZimmerman87 · Aug 01, 2013 at 09:21 PM 0
Share

When dealing with public static variables you need to be very careful to make sure you don't mess things up if you have multiple scripts accessing it.

Just because a value is static it does not mean it can't be changed. A public static variable can be easily changed by any scripts but there is only one instance of that variable in the entire program.

So for example you could use a public static variable for the player's health because there is just one player.

But you wouldn't want to use a public static variable for the enemies health because than all enemies would have the same health ins$$anonymous$$d of individual values.

I use them a ton though in my current project and find them an extremely fast way to do certain things.

Ya sorry my code is in C# I don't know JavaScript well enough to post my answers in it in any reasonable amount of time.

But if you want a public static variable you can do something like this:

 //ScriptExample1 contains the public static int
 
 public static int equal;
 
 //all your code to get the value of equal for this script
 
 
 
 //SEPARATE SCRIPT ScriptExample2 also uses the public static variable.
 
 //Example:
 
 int newInt;
 
 void Update()
 {
 newInt = ScriptExample1.equal + //anything else you might want to do with it
 }
avatar image
1

Answer by Jamora · Aug 01, 2013 at 09:11 PM

I see two choises:

If your GUIText string is actually just an int, you can use int.Parse(GUITextVar.text) to get the int value.

If there is more than just a number, say "13 apples" you would do better to store the int separately from the string and then do a string composition when you want to display it.

I'm thinking:

     var GUITextVar : GUIText;
     var intvar : int = 0;
     var currentval : int; //this is where the current value is stored
     car guiTextString : String = "Apples";
      
     function Update(){
     intvar+=1;
     currentval += intvar;
     GUITextVar.text=currentval.ToString() + " " + guiTextString;
     }
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

16 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Decreasing a value as another value increases through script 4 Answers

Changing a public varible from another script 3 Answers

Public variable hidden in the inspector 2 Answers

Problem About Get and Set Methods 0 Answers

Inventory system. Set multiple values/layers to one variable. 0 Answers


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