- Home /
Changing the text of an instantiated game object
This is a more specific question to one I made earlier that was never answer. I'm hoping since the scope is more specific, I'll have more luck.
I have the following text that takes a prefab and places it in a list of game objects (since I'm watering this down from my main script) and then places it inside the canvas at the spot I tell it to.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
public class TesterScript : MonoBehaviour {
public GameObject canvas;
public GameObject panel;
public List<GameObject> Plant_panel_list = new List<GameObject>();
int [] placement = new int[] {250,300};
// Use this for initialization
void Start () {
int xspot = placement[0];
int yspot = placement[1];
Plant_panel_list.Add((GameObject)Instantiate(panel));
Plant_panel_list[0].transform.SetParent(canvas.transform);
Plant_panel_list[0].transform.SetAsFirstSibling();
Plant_panel_list[0].transform.position = new Vector3(xspot,yspot,0);
}
// Update is called once per frame
void Update () {
}
}
Inside this prefab, there are 2 text game objects called Text1 and Text2. my question is how do I access those objects so I can control the text.
THank you in advance for any help.
Answer by TreyH · Feb 14, 2018 at 09:14 PM
Prefabs preserve their inspector assignments. Just have assignable values
[SerializeField] private Text text1;
[SerializeField] private Text text2;
and assign those children in your inspector. From then on, you can change their text with:
this.text1 = "Set me";
this.text2 = "to anything";
I honestly never would have gotten to this specific answer but this works exactly how I need it to.
I was about to just make a new text prefab for each text that I needed and instantiate them as a child of the original instantiated object. But thank you, you just saved me another day or two of headaches.
Answer by Deathdefy · Feb 14, 2018 at 07:31 PM
This should give you what you need. You need to create an extra component to be placed on your panel object. After this you reference the components you need, I.E. your text components and change their text this way.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TesterScript : MonoBehaviour
{
public GameObject canvas;
public GameObject panel;
public List<GameObject> Plant_panel_list = new List<GameObject>();
int[] placement = new int[] { 250, 300 };
// Use this for initialization
void Start()
{
int xspot = placement[0];
int yspot = placement[1];
Plant_panel_list.Add((GameObject)Instantiate(panel));
Plant_panel_list[0].transform.SetParent(canvas.transform);
Plant_panel_list[0].transform.SetAsFirstSibling();
Plant_panel_list[0].transform.position = new Vector3(xspot, yspot, 0);
//here are the changes
TestPanel testPanel = Plant_panel_list[0].GetComponent<TestPanel>();
testPanel.SetTextOne("Here is the value of test one");
testPanel.SetTextTwo("Here is the value of test two");
}
// Update is called once per frame
void Update()
{
}
}
//place this on your panel object
public class TestPanel : MonoBehaviour
{
//assign these in the inspector of your prefab
public Text TextOne;
public Text TextTwo;
public void SetTextOne(string val)
{
TextOne.text = val;
}
public void SetTextTwo(string val)
{
TextTwo.text = val;
}
}
I get the following error
Assets/TesterScript.cs(28,59): error CS0309: The type
TestPanel' must be > convertible to
UnityEngine.Component' in order to use it as parameterT' in > the generic type or method >
UnityEngine.GameObject.GetComponent()'
and since I'm not really following your code very well I don't even know when to begin troubleshooting that.
You need a way to access the Text components off of your objects. Without adding another component(I.$$anonymous$$ TestPanel) to your objects so that they can store a reference to those Text components you will manually have to find those text components by scouring through the object heirarchy to find those text objects.
When I try using this script though I don't see any inputs in the inspector, I just have this error.
I also updated my answer so the TestPanel now inherits from $$anonymous$$onoBehaviour, which is possibly part of the issue I gave you.
Answer by · Feb 14, 2018 at 07:33 PM
I think GameObject.Find("Text1");
should do? :D
Issue with this is he possibly has more than one "Text1" because its a prefab. At that point he won't be able to control which one hes actually changing if they are all on at the same time.
Thats true, so if they are Children of this game object, you can also use GetComponentsInChildren. But referencing them is the cleanest way of using it.
can you expand on this? I figured it was something allow this line but I wasn't able to figure out the exact syntax. I tried adding
var varholder = Plant_panel_list[0];
var textHolder = varholder.gameObject.Find("Text1");
but I got the error
Assets/TesterScript.cs(25,55): error CS0176: Static member `UnityEngine.GameObject.Find(string)' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d
You have to use GameObject.Find, gameObject is reference, GameObject is type :)
Answer by Bilgisoft · Nov 18, 2019 at 07:51 PM
You can access any child objects in intantieted object and you can change text properties.
Exmp:
foreach (Transform child in parent.transform)
{
if(child.name == "bla bla bla")
{ child.GetComponentInChildren<Text>().text = "lorem ipsum";
}
}