Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by dikabeast · Sep 21, 2014 at 11:14 PM · canvasimage4.6

[4.6 - UI] Accessing Text in Image inside Canvas via C# Script

Hi all, I'm newbie in Unity 4.6 I use canvas for my UI, and inside the canvas I have an image has text. I want to show the game score that change periodically on that text. How I do that? I mean how I access the text in the image and change it via script? I already read the topic http://answers.unity3d.com/questions/777335/46-ui-changing-the-text-component-via-script.html , but I still can't get the answer, I hope someone can solve this. Many thanks!

alt text

textimage.png (33.2 kB)
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
4
Best Answer

Answer by Erisat · Sep 21, 2014 at 11:44 PM

Create a public GameObject variable in one of your scripts, and then drag the gameobject containing the Text component into that variable in the inspector.

 public GameObject textgameobject;//set this is inspector or by script, maybe GameObject.Find or something, idk.
 void Foo()
 {
 Text text = textgameobject.GetComponent<Text>(); //get the text component in the gameobject you assigned
 text.text = "insert some text here"; //set the text in the text component
 }
 
   
Comment
Add comment · Show 6 · 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 dikabeast · Sep 22, 2014 at 01:24 AM 0
Share

Thank you for the answer but the text is in image properties, inside the canvas. So, I should put gameobject inside canvas? Ok, I'll try it first

avatar image Erisat · Sep 22, 2014 at 01:28 AM 0
Share

Well I can see that you have a text component. An Image in the new UI is just an image component on a gameobject, so that object isnt an image, its just an object with an image component. Even though they have those new recttransforms, theyre still GameObjects. Do the above, and drag the "ket_JmlPendapatan" object in hierarchy into the inspector variable for the public gameobject textgameobject in the above code. It will then use GetComponent to get the Text component and you can manipulate the text.text string.

avatar image dikabeast · Sep 22, 2014 at 02:30 AM 0
Share

SOLVED! Thanks a lot. I missunderstand about image and text before.

avatar image Erisat · Sep 22, 2014 at 03:14 AM 0
Share

Good to hear :) if you want, check out 3dbuzz's tutorial on the new UI. they do a very good job of explaining how everything works. I lost the link, but it's on 3dbuzz.com mainpage. The unity videos are decent, but 3dbuzz explains things in much more detail. Plus, they make me laugh sometimes. $$anonymous$$eeps it interesting.

avatar image nikprabha902gmail-com · Feb 24, 2016 at 01:08 PM 0
Share

Solved my problem, Thanks a lot.

Show more comments
avatar image
0

Answer by tvranjith · Mar 13, 2015 at 06:17 PM

See http://chikkooos.blogspot.jp/2015/03/new-ui-implementation-using-c-scripts.html

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 arjanstakie · Jun 12, 2017 at 09:52 PM

Hey i've solved the same problem but a little diffrent now (12 juli 2017). Instead of GetComponent< Text>(). I had to use Getcomponent< GUIText>(). The rest is exactly the same as @Erisat's

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 MagicStyle · Oct 24, 2017 at 12:18 PM 0
Share

I think you mean GetComponent< GUIText>(). But even this does not work for me.

avatar image arjanstakie · Oct 25, 2017 at 02:24 PM 0
Share

are you, "using UnityEngine.UI;" all the way on the top of your script under "using UnityEngine;" & "using System.Collections;" ? I also refer to the text with "public Text something;" ins$$anonymous$$d of using GetComponent<>(), So maybe it is GetComponent < Text>() after all.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

4.6 UI Image Color.Lerp 0 Answers

Trying to find() an image in canvas & getting NullReferenceException 1 Answer

Add smooth parts to UI.Image 1 Answer

Force UIImage to stay on viewport? 0 Answers

How to get mouse location on world canvas? [Unity 4.6] 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