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
0
Question by 0mr_ashutosh0 · Dec 11, 2014 at 03:36 PM · uitextcanvasgetcomponent

Multiple Text of canvas with One script ??

Hi, What i am trying to do is to setup a controller GameObject which will pass a custom text to other Text object.

I NEED TO HAVE DIFFERENT SCRIPTS ATTACHED TO DIFFERENT TEXT GAMEOBJECT, WHICH I WANT TO AVOID instead I WOULD LIKE TO SETUP A WAY IN WHICH THE CONTROLLER SENDS CUSTOM TEXT TO DIFFERENT TEXT GAMEOBJECTS FROM ONE PLACE.

alt text

![alt text][2]

Script one.cs is attached to MessageOne Script Two.cs is attached to MessageTwo

 // TextController 
 
 using UnityEngine;
 using System.Collections;
 
 public class TextController : MonoBehaviour {
 
     public GameObject textOne;
     public GameObject textTwo;
 
     private One one;
     private Two two;
 
     // Use this for initialization
     void Start () {
         one = textOne.GetComponent<One> ();
         two = textTwo.GetComponent<Two> ();
     }
     
     // Update is called once per frame
     void Update () {
         one.txt.text = "Sent from Controller to first ";
         two.txt.text = "Sent from Controller to second";
     }
 }
 
 
 // One
 
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class One : MonoBehaviour {
 
     public Text txt;
 
     // Use this for initialization
     void Start () {
         txt = GetComponent<Text> ();
     }
     
     // Update is called once per frame
     void Update () {
         txt.text = "First Message";
     }
 }
 
 
 // Two
 
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Two : MonoBehaviour {
     
     public Text txt;
     
     // Use this for initialization
     void Start () {
         txt = GetComponent<Text> ();
     }
     
     // Update is called once per frame
     void Update () {
         txt.text = "Second Message";
     }
 }
 

How can i use just a single script instead of two ( One.cs, Two.cs ). I have an Idea that i have to make some variable public and attach my script to both TextObjects and assign Them to script itself. and then from Controller Object i have to refer this script. I have tried several combination of GetComponent, Public, private etc but i can seems to get this thing done right. Please help guys.

problem.png (158.3 kB)
one.png (176.1 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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by jenci1990 · Dec 11, 2014 at 06:29 PM

I don't know but it's not right:

 // TextController 
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TextController : MonoBehaviour {
     public Text textOne;
     public Text textTwo;
     
     
     // Update is called once per frame
     void Update () {
         textOne.text = "Sent from Controller to first ";
         textTwo.text = "Sent from Controller to second";
     }
 }
 


Just drop the two Text variable in the inspector,and you don't need more script.

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 0mr_ashutosh0 · Dec 11, 2014 at 06:43 PM 0
Share

@jenci1990 what the hell i was thinking ? haha thanks it worked !!! i made some unnecessary assumptions in my script .

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

26 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

Related Questions

Canvas Button persistence 0 Answers

Text smaller on higher resolution screen than on lower resolution screen 1 Answer

UI text vs. moving camera 1 Answer

How to change color of text relative to current color? 1 Answer

Unity UI Text with markup tags - “String too long for TextMeshGenerator” 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