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
2
Question by obiolg · Mar 15, 2015 at 07:42 PM · scripting beginnerreference-other-objectgeneral

Access Text inside Canvas by name

I have a Canvas with two Text elements inside, like this:

Canvas:

  1. FooText

  2. BarText

I would like to attach a C# Script to the Canvas and access both the Text objects from inside the script. Something similar to this:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DisplayScore : MonoBehaviour {
 
     Text fooText;
         
     void Start () {
         fooText = gameObject.GetComponent<Text>(); // ???? 
         fooText.text="Change the text";
     }
 }

Basically, how would I access text element by its name? The question is trivial, but I am new to Unity and after digging tons of other answers need some 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
5
Best Answer

Answer by fafase · Mar 15, 2015 at 08:19 PM

Your objects are children of your canvas and the script is attached on the canvas so you can look in the Transform.

 Transform child =  transform.Find("ObjectName");
 Text t = child.GetComponent<Text>();
 // You know the rest
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 obiolg · Mar 15, 2015 at 08:29 PM 0
Share

Thank you! Exactly what I was looking for.

avatar image
2

Answer by badatnames16 · Mar 15, 2015 at 08:01 PM

Just replace line 7 with

   public GameObject fooText;

and in the inspector drag the text into the slot. Then put

 Text footext = fooText.GetComponent<Text>();
 fooText.text = "Change the text";

This worked for me.

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 obiolg · Mar 15, 2015 at 08:13 PM 1
Share

Thank you. I know this works. But imagine that I have 10 elements inside. I was wondering if it is possible to get reference to the objects dynamically without having to make them public and drag-and-drop them manually.

avatar image Abdullah6 · Jun 14, 2021 at 07:06 PM 0
Share

You forgot to put the [SerializeField] on the first variable, so it should actually be:

[SerializeField] public GameObject fooText;

without the [SerializeField] you won't be able to see the variable in the inspector

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

Attempting to Increase Ammo by Killing Enemy 1 Answer

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

Having trouble coding an on/off toggle for an animation in script. Please help! 1 Answer

How to download unity 4.6 manual 1 Answer

triggering animation when conditions are met in battle chess 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