- Home /
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.

 ][2]
][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.
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.
@jenci1990 what the hell i was thinking ? haha thanks it worked !!! i made some unnecessary assumptions in my script .
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                