I want to assign GUIText from heirarchy into prefab using that C# code:

    private Text HighScoreText;

    void Start(){
 HighScoreText = GameObject.Find("HighScore").GetComponent();


But that works only if my present prefab object isn't prefab and is in hierarchy. What am I doing wrong?


I assume you want to save some scores to be used on the next game runs, right? If so it's not that straight forward. Basically when the game stops everything is lost. To make something be saved and used on the next runs you need serialization. On the other hand there is an easy to use but easy to cheat by players way - PlayerPrefs, you will need to use PlayerPrefs.SetFloat and PlayerPrefs.GetFloat or their Int counterparts SetInt and GetInt.

| improve this answer | |
  • \$\begingroup\$ I'm using PlayerPrefs.SetIng and GetInt to save score but I want to show it using GUIText. And that works if my player object is in heirarchy too, but not works when my player object isn't in heirarchy and is prefab, then I can't drag and drop into inspector panel my GUIText element to connect it with my player. So I wanted assign that GUI element into my player by the c# script and GameObject.Find... Sorry for bad eng \$\endgroup\$ – Igor Jul 1 '16 at 13:52
  • 1
    \$\begingroup\$ There is some problem with your overall structure. Player should not have any highscore related code. Separate Highscore and Player completely. You may create an Object called Highscore , it could have 2 ints, currentScore and highScore, make so that when currentScore is bigger than highscore then highScore = currentScore. Then attach GUIText to Highscore object and display highScore. Then you will only need to change currentScore. \$\endgroup\$ – Nikaas Jul 1 '16 at 14:16
  • \$\begingroup\$ Yeah, I removed all GUI code and elements from player script and I made another scripts for collect points, HS and display GUI and all works perfectly. Thanks again for advice. :) \$\endgroup\$ – Igor Jul 1 '16 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.