Search code examples
c#unity-game-engine

Unity GUI Text GameObject - Basic C# Script Not Working


I added a GUI text GameObject to a new project and attached a new C# script to it.

I want the GUI text GameObject to count up to 10000 (increment by 1 every frame) and stop when it reaches 10000. I used a for loop to try and achieve this, but I get the following error:

Assets/oText.cs(20,33): error CS0029: Cannot implicitly convert type 'int' to 'string'

What am I doing wrong?

My C# script is:

using UnityEngine;
using System.Collections;

public class oText : MonoBehaviour {

    // Use this for initialization
    void Start () {
        guiText.text = "GUI Text Area Test";

    }
    
    // Update is called once per frame
    void Update () {
        int myInt = 1;

        for(int i = 0; i < 10000; i++)
        {
            myInt = myInt + 1;
            guiText.text = myInt;
        }
    }
}

Solution

  • guiText.text is a string. myInt is an int. The conversion isn't implicit. You need to specify an explicit conversion:

    guiText.text = myInt.ToString();