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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to extract the variable in this code to display in a UI Text?

Discussion in 'Editor & General Support' started by MattCarter24, Jun 25, 2015.

  1. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    Hello,

    I have the code below which works perfectly. I would like to somehow (in a new scene) pull the score variable from the previous code and display it in a UI text. I would also like the new script in the new scene to run automatically without any triggers?

    Any help would be much appreciated!

    Here is my code...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class CheckAnsL8 : MonoBehaviour
    5. {
    6.    
    7.     public InputField iField;
    8.     string myName;
    9.     string myText;
    10.     public int score = 0;
    11.    
    12.     public Text Status;
    13.    
    14.     GameObject myTextgameObject; // gameObject in Hierarchy
    15.     Text ourComponent;           // Our refference to text component
    16.    
    17.     void Start()
    18.     {
    19.         if (PlayerPrefs.HasKey("Score") == true)
    20.         {
    21.             score = PlayerPrefs.GetInt("Score");
    22.         }
    23.        
    24.         // Find gameObject with name "MyText"
    25.         myTextgameObject = GameObject.Find("MyText");
    26.         // Get component Text from that gameObject
    27.         ourComponent = myTextgameObject.GetComponent<Text>();
    28.     }
    29.    
    30.     public void MyFunction()
    31.     {
    32.         Debug.Log(iField.text);
    33.         myName = iField.text;
    34.         if (myName == "castle")
    35.         {
    36.             score++;
    37.             Debug.Log("Correct! The word 'castle' is correct!");
    38.             Debug.Log("Your score is now:");
    39.             Debug.Log(score);
    40.             PlayerPrefs.SetInt("Score", score);
    41.             Status.text = "Correct!";
    42.             Status.color = Color.green;
    43.         }
    44.         else
    45.         {
    46.             score--;
    47.             Debug.Log("Incorrect! The answer was 'castle'.");
    48.             Debug.Log("Your score is now:");
    49.             Debug.Log(score);
    50.             PlayerPrefs.SetInt("Score", score);
    51.             Status.text = "Incorrect!";
    52.             Status.color = Color.red;
    53.         }
    54.        
    55.         GameObject theTargetChild = GameObject.Find("theTargetChild");
    56.         foreach (Transform child in theTargetChild.transform)
    57.         {
    58.             child.gameObject.SetActive(true);
    59.         }
    60.     }
    61.    
    62.     void Update () {
    63.         ourComponent.text = score.ToString();
    64.     }
    65. }
     
  2. ForceVFX

    ForceVFX

    Joined:
    Jan 21, 2011
    Posts:
    612
    Newpage.cs

    Using unity.ui

    Public text myTextbox;

    Drag new text box into slot.


    start()

    Get player prefs...

    myTexbox.text = My player Prefs value.toString();


    Done
     
  3. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    It didn't work
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Finish : MonoBehaviour {
    6.  
    7.     public Text myTextbox;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         GetComponent PlayerPrefs;
    12.         myTextbox.text = PlayerPrefs value.toString();
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.        
    18.     }
    19. }
    20.  
     
  4. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120
    FIXED IT! YAY
     
  5. ForceVFX

    ForceVFX

    Joined:
    Jan 21, 2011
    Posts:
    612
    I wanted to.....guide you....using intellisense, and your basic understanding..I knew you would get it..

    Good luck!

    P-

    Use awake to get 'em...start to set 'em..and always use..hasKey ..
     
  6. MattCarter24

    MattCarter24

    Joined:
    May 27, 2015
    Posts:
    120