Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 adulthood · Aug 11, 2015 at 03:56 PM · updatestring

How to add symbols to string?

Hello guys. Trying to make a script, which after pressing a button adds a letter to the string. Each button = one letter. Made simple script, so that GET is the result string, which i need. But the problem is when i press the button, it doesn't add new symbol to string, but "refreshes" it, so that it shows just a one symbol ("A", for example - the name of the button), instead of showing "AA...".

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;  
 
 public class Script : MonoBehaviour {
     public Button MyButton;
         
     
 
         void OnEnable()
         {
             
             MyButton.onClick.AddListener(MyFunction);
             
         }
         
 
         void MyFunction()
         {
         var GET = (this.gameObject.name);
             Debug.Log (GET);
         }
 
     }


Would be very thankful for any help)

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

2 Replies

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

Answer by nullgobz · Aug 11, 2015 at 04:03 PM

This symbol = means assign, as in it will take whats on the right and stuff it to the left. This is why you get the same value.

You also need to use a member variable, rather then your local variable "GET". If you wish to "save" your value across diffrent method calls.

Here is an example of adding "this.gameObject.name" to a string rather then assigning.

  using UnityEngine;
  using System.Collections;
  using UnityEngine.UI;  
  
  public class Script : MonoBehaviour
 {
      public Button MyButton;
      private string theString;
  
      void OnEnable()
      {
         MyButton.onClick.AddListener(MyFunction);
      }
      void MyFunction()
      {
         theString += (this.gameObject.name);
         Debug.Log(theString);
      }
 }
Comment
Add comment · Show 2 · 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 adulthood · Aug 11, 2015 at 08:11 PM 0
Share

thanks, this works fine! But got another problem - it defines each button apartly, when i press "A" it writes AAAA, when press "B" - BBBB... Could you please tell, how can i "unite" them, so that there would be the one string for "ABAABBBABBABAB..." etc.... Thanks a lot!

avatar image nullgobz · Aug 11, 2015 at 09:04 PM 0
Share

make the member string static: private static string theString;

avatar image
1

Answer by allenallenallen · Aug 11, 2015 at 03:58 PM

By typing this:

 var GET = (this.gameObject.name);

You're overriding GET.

I recommend you to create a new variable to keep track of all the symbols.

 public var tracker = ""; // Declared in the beginning of the script.

And to add more symbols, you do this:

 var GET = (this.gameObject.name);
 tracker += GET;

Note: I haven't tested this so the syntax might or might not work.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Create Texture2D and assign image to it through a script 1 Answer

How to Fire Shells With Speech Recognition Input 3 Answers

How to Update a score count going up and down 1 Answer

Update the GUI when string changes 2 Answers

Remove the last word from a string 2 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