Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

how is possible to show a ui score text in another scene ?

Discussion in 'Scripting' started by victor2dgames, Apr 3, 2016.

  1. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    how is possible to show a ui score text in another scene ? thanks,
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Create a UI with a text component in the other scene and display the value that you have saved in the old scene.

    Several options to "save" a value, some are persistent, some will only be there as long as the game is running:
    1) DontDestroyOnLoad // will be there as long as the GO exists
    2) PlayerPrefs // persistent
    3) static variables // as long as the game runs
    4) your own save/load system // persistent
     
    victor2dgames likes this.
  3. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    i did create a ui text , in another scene score is saved by this method :
    ScoreManager.Instance.TotalscoreCounter+=ScoreManager.Instance.ScoreCounter;

    PlayerPrefs.SetInt("BestTimes",ScoreManager.Instance.TotalscoreCounter);

    how this new ui text can get that value,i mean i should create new script for this new ui text , how ? thanks again.
    how dislay that old value? other scene value?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    PlayerPrefs.GetInt

    Just as easy as saving it to the player prefs.
     
    victor2dgames likes this.
  5. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    there is a ui text , brand new , how this is reading the value?
     
  6. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    please explain the steps in more details. thanks.
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You have to use the method that I've linked.

    It seems you should start out with some basics. This is really easy stuff.

    - use the method in the link and if you need it, store the value in an int variable
    - access the text's text and set it to be the value of the variable

    The example in the documentation already shows how to load an integer value from PlayerPrefs.
     
    victor2dgames likes this.
  8. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    thanks. after all the value only changes in inspector but ui text in the scene doesnt change. why? thanks.
     
  9. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    all is done, why there is an extra zero added to this new number?
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Post the script, otherwise I would have to guess even more.
     
    victor2dgames likes this.
  11. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    that is fixed by your help, now another problem : when i leave the scene and return back agian to the same scene i get this error and nothing is spawned :
    MissingReferenceException: The object of type 'RecycleGameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    ObjectPool.NextObject (Vector3 pos) (at Assets/Scripts/ObjectPool.cs:26)
    GameObjectUtil.Instantiate (UnityEngine.GameObject prefab, Vector3 pos) (at Assets/Scripts/GameObjectUtil.cs:15)
    GameManager.ResetGame () (at Assets/Scripts/GameManager.cs:83)
    GameManager.Update () (at Assets/Scripts/GameManager.cs:42)
    why?!!!
     
  12. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're trying to access a gameobject that no longer exists because it has been destroyed when another scene was loaded.
     
    victor2dgames likes this.
  13. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    how fix it? thanks.
     
    Last edited: Apr 3, 2016
  14. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    It seems the object pool does not reset when the scene changes, so it tries to hold references to objects that have been destroyed.
    I'm not sure where you've got the scripts from, but apparently they do either not handle that at all or you have to explicitly tell them to clean the pool when the scene changes.

    Another thing would be to keep all pooled objects alive, but that's a horrible idea.
     
    victor2dgames likes this.
  15. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    i got it fixed by deleting recycleGameObject script which was attached to 3 prefabs and after that there is no error. is this the correct way?
     
  16. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    How would I know?
    As I mentioned, I have no clue where that code comes from and how your project is set up. These objects may no longer be taken into account for object pooling. It's your game, you have to decide whether that's a good solution or not.

    But this will probably happen for other objects too, unless you remove the script everywhere, which would make the pooling system useless in the end.
    I assume you've got the script from the forum or some website, so there is probably also a way to clear the pool so that it no longer pools the objects which have been or will be destroyed.
     
    victor2dgames likes this.
  17. victor2dgames

    victor2dgames

    Joined:
    Mar 26, 2016
    Posts:
    77
    you are a master , as i removed recyclegameobject script all errors gone. thanks my friend . source script is from
    Lynda-Unity.5.2D.Essential.Training.