Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 wolgy88 · Feb 14, 2018 at 07:25 PM · c#instantiatetext

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.

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

4 Replies

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

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";
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 wolgy88 · Feb 14, 2018 at 09:38 PM 1
Share

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.

avatar image
0

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;
     }
 
 }
 
 
Comment
Add comment · Show 6 · 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 wolgy88 · Feb 14, 2018 at 07:47 PM 0
Share

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 parameter T' 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.

avatar image Deathdefy wolgy88 · Feb 14, 2018 at 08:08 PM 0
Share

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.

avatar image wolgy88 Deathdefy · Feb 14, 2018 at 08:13 PM 0
Share

When I try using this script though I don't see any inputs in the inspector, I just have this error.

Show more comments
avatar image Deathdefy wolgy88 · Feb 14, 2018 at 08:10 PM 0
Share

I also updated my answer so the TestPanel now inherits from $$anonymous$$onoBehaviour, which is possibly part of the issue I gave you.

avatar image
0

Answer by · Feb 14, 2018 at 07:33 PM

I think GameObject.Find("Text1"); should do? :D

Comment
Add comment · Show 4 · 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 Deathdefy · Feb 14, 2018 at 07:34 PM 0
Share

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.

avatar image · Feb 14, 2018 at 07:42 PM 0
Share

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.

avatar image wolgy88 · Feb 14, 2018 at 08:05 PM 0
Share

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

avatar image wolgy88 · Feb 15, 2018 at 09:24 AM 0
Share

You have to use GameObject.Find, gameObject is reference, GameObject is type :)

avatar image
0

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";
          }
    }


Comment
Add comment · 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

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

453 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

trouble instantiating ui text 1 Answer

Instantiate and edit a text box during runtime 1 Answer

Instantiate prefab text into Canvas 2 Answers

How to access value within instantiated class in different object? 1 Answer


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