Using Input Field to change Text of all Text Objects in Array

Hey all,
I’m trying to make an input field that can output text to several different Text UI objects, I want to use an array for this, So i can continually add new Text objects to output to.

The point of this: is to let me test large numbers of fonts at the same time. I use photoshop a lot for creating Logos for clients and I really dont enjoy going one by one testing each different kind of font. So i figured I could use unity to input the text just once then see all of my fonts on the screen at once in a scrolling window.

My problem is ive never tried to understand for each loops and the way they effect arrays.

heres the code I tried to make work, btw I understand why this doesnt work, its because “numOfTextDisplayers” is telling the code to only change the Text object in position ‘0’ in the array, my for each loop is basically doing nothing, but i dont know how to make every Text object in the array update to the input fields Text when the function is called.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class InputToDisplay : MonoBehaviour
{
public string inputFieldContent;
public GameObject inputField; //this does NOT refer to the text input field object, but the child named Text that comes with the inported input field
public GameObject[ ] textDisplay;
public int numOfTextDisplayers = 0;

public void UpdateText()
{
inputFieldContent = inputField.GetComponent().text;
foreach (GameObject i in textDisplay) {
textDisplay[numOfTextDisplayers].GetComponent().text = inputFieldContent;
}
}
}

When using a foreach loop, you target i in this case.

i.GetComponent

If you read the foreach statement to yourself, it might help to understand it.

For each GameObject i in textDisplay, do this

1 Like

Holy moly that worked like a charm!! thank you for taking the time to help me understand how i was messing this up!!