Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Scripting to Multiple Text Fields

Discussion in 'Scripting' started by Teriander, Feb 7, 2015.

  1. Teriander

    Teriander

    Joined:
    Jan 12, 2012
    Posts:
    113
    Ok, I'm trying to use a script to code my text fields, but I have multiple text fields. How do I get my text variable to look at a certain text field? For example, this is what I have now to find one text field:
    Code (CSharp):
    1. Text strikePackage = FindObjectOfType<Text>();
    2. strikePackage.text = "This is a test.";
    This works fine for a single text field. But how do I target a specific text field that's called - "TextPackage" ?
     
  2. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    107
    One way is to find all objects:

    Object.FindObjectsOfType


    But what I guess will be good for you is to create:

    Code (CSharp):
    1. public Text TextPackage;
    You will now be able to link you text package in the Editor and:

    Code (CSharp):
    1. TextPackage.text="This is a test";
    Or if this text packages are Instantiated I would try to store references: Inside of script for TextPackage I would create:

    Code (CSharp):
    1. public MyTextManagerScript myStorage;
    And there I would create an array of Text field refferences and function to store them. Every time TextPackage is created he would call storage he is linked to and store reference to himself there.
     
  3. Teriander

    Teriander

    Joined:
    Jan 12, 2012
    Posts:
    113
    I'm not sure if any of these would work for my case. Basically I have multiple text fields of different sizes in different locations with different game object names, using 4.6. Picture an interface with multiple text information that will popup.

    I dont think I can use Instantiate since the text is of different locations already created under my Canvas. Or will it? New to creating this type of interface in 4.6.
     
  4. quizcanners

    quizcanners

    Joined:
    Feb 6, 2015
    Posts:
    107
    Here is one more option:
    Create a script:

    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class MyChangableText: MonoBehaviour {
    4. Text instruction;
    5.  
    6. void Start()
    7.         {
    8.             instruction = GetComponent<Text>();
    9.         }
    10.  
    11. public void ChangeMyText(string NewText)
    12. {
    13. instruction.text=NewText;
    14. }
    15.  
    16. }
    Assign this script to every text field you have. Now in the script/scripts that are controlling your game write:

    Code (CSharp):
    1. public MyChangableText scoreField;
    2. public MyChangableText ammoField;
    3. public MyChangableText healthField;
    4. public MyChangableText someOtherfieldField;
    5.  
    And in the Update function of this script:

    Code (CSharp):
    1. void Update () {
    2. ammoField.ChangeMyText("Ammo: "+ ammoCount);
    3. }
    And now you can connect your text Game Objects to your controlling scripts in the Editor and everything will work.

    But keep in mind, you don't want to change your text every frame, it will do no good to your performance. So something like:
    Code (CSharp):
    1. if (shootsFired){
    2. ammoField.ChangeMyText("Ammo: "+ ammoCount);
    3. shootsFired=false;
    4. }
    would be good.
     
    Last edited: Feb 8, 2015
    Teriander likes this.
  5. Teriander

    Teriander

    Joined:
    Jan 12, 2012
    Posts:
    113
    Thanks for the feedback. This is resolved now!