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 /
This question was closed Apr 08, 2018 at 03:15 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by tperry1x · Oct 25, 2015 at 10:27 PM · textcanvas

Finding a GameObject using a canvas text?

I just don't understand it. I used to use guitext and managed to muddle through with that. I've got a canvas, called "Canvas" and a text ui element called "text" – think these are the defaults.

With javascript, I just want to be able to affect the value of the text shown. How do I do this? I've googled and tried to use their examples, but I either get an error of just nothing happens.

Please help. Tearing my hair out over what should be so, so so simple.

Comment
Add comment · Show 4
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 tperry1x · Oct 25, 2015 at 10:40 PM 0
Share

Not solved. What I'm trying to do is to find a gameobject (in this case, text with a specifc name in a canvas).

 var myDescText : GameObject;
  myDescText = GameObject.Find("NotifyText");

  // for the purposes of this question, the variable myLitem contains a string "redmist"
 // so, with this line, the text of the gameObject called 'NotifyText' should be changed to 'redmist' right?
   myDescText.GetComponent <Text> ().text = myLitem;

Except it doesn't work and spits out 3 errors all pointing to the last line of my code. These errors are:

Assets/scripts/switchscript.js(65,35): BCE0043: Unexpected token: ).

Assets/scripts/switchscript.js(65,36): BCE0044: expecting ), found '.'.

Assets/scripts/switchscript.js(65,37): UCE0001: ';' expected. Insert a semicolon at the end.

Please can someone see where I'm going wrong and offer corrections as needed.

avatar image KillHour tperry1x · Oct 27, 2015 at 03:42 AM 0
Share

There isn't enough context here to deter$$anonymous$$e what's wrong. You have a syntax error somewhere. Can you post the code near line 65?

avatar image fafase tperry1x · Oct 27, 2015 at 06:43 AM 1
Share

You are using Javascript (unityScript) it does not contain a generic version of GetComponent.

     myDescText.GetComponent (Text).text = myLitem;
avatar image tperry1x fafase · Oct 27, 2015 at 07:08 AM 0
Share

Thanks fafase. So, what do I need to use ins$$anonymous$$d in unityscript/javascript?

Just to clarify bshalke, I've started afresh with a new script, which throws the errors.

pragma strict

var myDescText : GameObject; myDescText = GameObject.Find("NotifyText");

function Start () { myDescText.GetComponent ().text = "testing"; }

function Update () {

}

Throws the errors:

Assets/scripts/textscript.js(7,33): BCE0043: Unexpected token: ). Assets/scripts/textscript.js(7,34): BCE0044: expecting ), found '.'. Assets/scripts/textscript.js(7,35): UCE0001: ';' expected. Insert a semicolon at the end.

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Oct 27, 2015 at 07:26 AM

You can use Javascript but just use it right.

 var myDescText : GameObject;
 function Start () {
    myDescText = GameObject.Find("NotifyText");
    if(myDescText != null){
        var text : Text = myDescText.GetComponent (Text);
        if(text !=null){
              text.text = "testing"; 
        }
    }
 }

The null checks are not required but you can also add some debug to see if something goes wrong.

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 tperry1x · Oct 27, 2015 at 09:09 AM 0
Share

Thank you fafase. Exactly what I was looking for.

avatar image
0

Answer by KillHour · Oct 26, 2015 at 09:46 AM

The "Text" component has a property called "text"

That's the one you need to change.

 var myTextObject : GameObject;
 
 //Assign your object
 
 myTextObject.GetComponent <Text> ().text = "Your text here."

http://docs.unity3d.com/ScriptReference/UI.Text-text.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

Follow this Question

Answers Answers and Comments

32 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

Related Questions

How to attach a canvas to a game object properly? 1 Answer

Is It Possible to Render the same Data to 2 Different UI Text Objects? 1 Answer

Text Appears in Inspector but Not on Screen 0 Answers

Canvas UI Priority layers 1 Answer

Quick Question on UI Text Alignment 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