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
1
Question by TheNicojack · Jan 03, 2018 at 12:17 PM · buttonsstringsnumbers

Using Buttons to write numbers

Hello everyone. So I have 10 buttons labled with the numbers 0-9, I managed to let the buttons write their numbers into a textfield when I click on them (with the simple OnClick event implemented in unity), but everytime I click on a new button, the previous number gets overwritten and I need it to remain to be able to show numbers like 12, 34 and so one. Unfortunately I dont even know how to properly start the C# Script for that. I hope that some of you can help me. ^^

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
1

Answer by OneCept-Games · Jan 03, 2018 at 12:20 PM

When you assign the button value to your textfield add it instead of assigning it.
Like: textfield.text += string.Format("{0}", button.text);

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
1

Answer by tormentoarmagedoom · Jan 03, 2018 at 01:18 PM

Hello @TheNicojack :

As @OneCept-Games says, you can use this "formula"

 FinalText.text = FinalText.text + NewText.text;

so the new characters will be writen after the old characters.

This "formula is exactly the same as this (short way)

 FinalText.text += NewText.text;

This ( + = ) can be used with all variables like int or float, to add or remove ( - = )

you can also check the length of the string variable to decide if want to create a new string or continue adding the new text to the old with:

 int NumberOfCharacters = FinalText.text.Length;

For remove characters from a string variable you need to use

 string NewText = FinalText.text.Substring (int StartIndexCharacter);

More info in https://answers.unity.com/questions/202004/substrings.html

Good luck with your project!

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 TheNicojack · Jan 05, 2018 at 02:04 PM

@tormentoarmagedoom @OneCept-Games Thanks for the help ^^ now my code looks like this:

public class T1Btn : MonoBehaviour {

 public Text FinalText;
 public Text Btn1;
 public Text Btn2;
  

 
 void ButtonText()
 {
     FinalText.text = FinalText.text + Btn1 + Btn2;
 }

}

but it isn't working, can you tell my hat I am doing wrong ?

Comment
Add comment · Show 5 · 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 OneCept-Games · Jan 05, 2018 at 04:02 PM 1
Share

You are assigning Text object tro a string. Always make sure you are assigning same Type's, except when you use the object type. And also always check for objects not assigned in the Inspector, like: Debug.Assert(Btn1 != null, "Btn1 is not assigned"); Your code should be:

 FinalText.text = FinalText.text + Btn1.text + Btn2.text;

avatar image TheNicojack OneCept-Games · Jan 07, 2018 at 05:09 PM 0
Share

Alright thx for that ^^ now the only problem is, that I can't select my function in the inspector of the buttons. I attached the script on the $$anonymous$$ain Camera, added the text segments of the buttons aswell as the textfield ,where everything should get written in, in the Btn1, Btn2, Btn3 and so on fields in the inspector and added the main camera (containing the script) to the on click events of the buttons, but when I want to select the function I wrote, I can select the T1Btn function but the ButtonText function I wrote I can not select. Did I make a mistake there ?

P.S. I hope you understand what I mean ^^

avatar image TheNicojack OneCept-Games · Jan 11, 2018 at 01:04 PM 0
Share

@OneCept-Games

Alright thx for that ^^ now the only problem is, that I can't select my function in the inspector of the buttons. I attached the script on the $$anonymous$$ain Camera, added the text segments of the buttons aswell as the textfield ,where everything should get written in, in the Btn1, Btn2, Btn3 and so on fields in the inspector and added the main camera (containing the script) to the on click events of the buttons, but when I want to select the function I wrote, I can select the T1Btn function but the ButtonText function I wrote I can not select. Did I make a mistake there ?

P.S. I hope you understand what I mean ^^

avatar image Lethael TheNicojack · Jan 11, 2018 at 01:17 PM 1
Share

@TheNicojack You need to make ButtonText() public in order for you to be able to select it in the inspector ;)

You wrote it as:

 void ButtonText () {
 // Blah blah blah
 }

To make it public it should be:

 public void ButtonText () {
 // Blah blah blah
 }

I assume you're new to program$$anonymous$$g?

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

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

Related Questions

Remote KeyPad with Buttons 1 Answer

How Do I Make My Score Add Numbers Instead Of Strings? 2 Answers

Dividing On strings CS0019 1 Answer

How to connect the strings i get ftom pressing buttons 1 Answer

Moving buttons 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