Home > front end >  How can I get this to work : "WorkersText = GameObject.FindGameObjectWithTag("Workers"
How can I get this to work : "WorkersText = GameObject.FindGameObjectWithTag("Workers"

Time:07-12

I have a text object in my game called WorkersText. I am creating a save / load script for my game so the player can save and load their progress.

Originally I dragged the Text Object to the Object Inspector, but after the player loads the save point, it removes it from there. The same happened with general GameObjects, so I added a line to assign it in Start(). eg ObjectManager = GameObject.FindGameObjectWithTag("ObjectManager");

Now I need to do the same with my Text objects but it wont let me do WorkersText = GameObject.FindGameObjectWithTag("Workers");

I get the error that Cannot convert type 'UnityEngine.GameObject' to 'UnityEngine.UI.Text'

So I assume FindGameObjectWithTag is off the table. Is there any other way to refrence the Text via code? Sadly as mensioned before, I cannot simply drag the Text object into the Object Inspector.

CodePudding user response:

You get GameObject as a result and obviously cannot assign it to the Text type variable. That is what error is about. Add GetComponent<Text>() to the object returned. If it has such a component - you will get your Text to assign.

  • Related