0
votes

i am currently having an issue in unity where i am outputting numbers into a GUIText and for some reason when the number changes it writes the new number on top of the old one instead of changing the number.

I have no idea why this is happening as i only have 1 GUIText that i am writing to, the script that is doing the writing is only on one object and i am changing the variable that is currently writing to the GUItext rather than switching it for a new one.

Any help on this would be greatly appreciated thanks.

Code being called:

public class RNG : MonoBehaviour {

    public GUIText thisAnswer;
    public RaycastHit hit = new RaycastHit ();
    public Camera mycam;
    int randomNumber = 0;
    int miniScore = 0;
    int CorrectCount = 0;
    int refreshCount = 0;

    // Use this for initialization
    void Awake ()
    {


    }

    void Start ()
    {

    }

    void FixedUpdate ()
    {
            refreshCount += 1;




    }
    // Update is called once per frame
    void Update ()
    {

            if (CorrectCount != 5) {
                    StartCoroutine (Selection ());
                    thisAnswer.text = "" + randomNumber;
                    if (refreshCount % 200 == 0) {
                            Refresh ();
                    }
            } else {
                    PlayerPrefs.SetInt ("MiniScore", miniScore);
                    Destroy (GameObject.Find ("Killswitch"));
            }

    }

    IEnumerator Selection ()
    {

            if (Camera.main != null) {
                    Ray ray = mycam.ScreenPointToRay (Input.mousePosition);

                    if (Input.GetKeyUp (KeyCode.Mouse0)) {

                            if (Physics.Raycast (ray, out hit)) {

                                    if (hit.transform.tag == "answer") {
                                            if (System.Convert.ToInt32 (thisAnswer.text) % 3 == 0) {
                                                    miniScore = miniScore + 100;
                                                    CorrectCount = CorrectCount + 1;
                                                    randomNumber = Random.Range (0, 36);
                                                    yield return new WaitForEndOfFrame ();

                                            } else if (System.Convert.ToInt32 (thisAnswer.text) % 3 != 0) {

                                                    if (miniScore > 50) {
                                                            miniScore = miniScore - 50;
                                                    } else if (miniScore < 50) {
                                                            miniScore = 0;
                                                    }
                                                    randomNumber = Random.Range (0, 36);
                                                    yield return new WaitForEndOfFrame ();
                                            }
                                    }
                            }
                    }
            }
    }

    void Refresh ()
    {

            randomNumber = Random.Range (0, 36);


    }

}

1
Hi, just to be sure it might be worth putting a log message in the Start() method and see if somehow there are more than 1 instances of your script getting createdGarrett Hussey
Just checked that and it is only running once.Jed

1 Answers

1
votes

I have sorted it now, it turns out whilst i was only calling the script once i was loading the level that the script was in twice...hehe oops. :)