Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
12 Jun 22 - 14 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 Zitoox · Nov 18, 2016 at 12:04 AM · c#scripting problemtextchangeonclick

Change text value

I am just starting to write C# and i would use Javascript if i could because my knowledge about C# specifically is "small".

So, i have a UI Text, and i am trying to make it add 1 to the score each time the player clicks a button. For example, the score is 0. The player clicks the button, and it is changed to one. If he clicks again, it changes to two and it continues. I have tried A LOT OF THINGS but i always got myself in the same two problems: i can't reference something or i can't understand how it works so i can make it. I think i am in the right path, but i couldn't really find anything related to how to reference texts and how to make a variable for it, and i am also struggling in making a trigger for it.

Here is my code for now:

 using UnityEngine;
 using System.Collections;
 
 public class ObjClick : MonoBehaviour
 {
     bool click = false;
 
     public GameObject fall;
     public GameObject txt; //I wanted to use this variable to hold the text, but as i said i couldn't reference it. There isn't exactly something like "public Text txt".
     public Animator anim;
     public int score;
 
     void Start()
     {
         fall.SetActive(false);
     }
 
 
     void Update()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             anim.SetTrigger("Action");
             fall.SetActive(true);
             //I plan to make something like, on left mouse click, add one to the score.
         }
     }
 
     void UpdateScore()
     {
         txt.text = score; // <- This is my main problem. I don't know how to reference the text. I always get the error "Type 'UnityEngine.GameObject' does not contain a definition for text" etc...
     }
 }

Could someone help me to finish this? It's very important. Any help would be appreciated. Btw this is my last question.

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 Namey5 · Nov 18, 2016 at 04:44 AM 0
Share

Which do you mean by 'UI Text'? Is this as part of an nGUI canvas, a legacy GUI Text component, or a custom script? Each is accessed differently. Side note; text isn't a member of the GameObject class, you would access it like most components in Unity 5, i.e.

txt.GetComponent();

You would also want to add ".ToString()" after the 'score' assignment, otherwise you'll get an error there.

avatar image Zitoox Namey5 · Nov 18, 2016 at 02:26 PM 0
Share

It's a text, and it is a child of canvas. Normal text.

3 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by Prezzo · Nov 18, 2016 at 02:38 PM

To start, Include UnityEngine.UI so write using UnityEngine.UI on top of your script.

Next, change public GameObject txt; by public Text txt; In the editor, drag your text object into the text slot of your script.

Access your text of your Txt object by typing txt.text = 'blah blah text'. For displaying numbers, you need to convert them to a string first, so write

 txt.text = yourNumberVariable.ToString()





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 grange9 · 13 hours ago 0
Share

I got stuck at the "In the editor, drag your text object into the text slot of your script." because Unity says I am unable to drag it. Please help :'(

avatar image
1

Answer by ElijahShadbolt · Nov 18, 2016 at 06:20 AM

 txt.GetComponent<UnityEngine.UI.Text>().text = score.ToString();
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 giantkilleroverunity3d · Jan 18, 2019 at 06:43 PM

@Zitoox The text box is in the inspector panel of Unity. Thought I would state this as the OP comment posted that

There isn't exactly something like "public Text txt".

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

254 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers

Making texts appear/disappear after having completed a task 0 Answers

Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 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