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

Removing a TextField

Discussion in 'Scripting' started by GamesOnAcid, May 11, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         if (hammerIsInRange == true)
    4.         {
    5.             collectMessage = GUI.TextField (new Rect (10, 10, 200, 20), collectMessage, 25);
    6.             if(inventory.hammerIsCollected == true)
    7.             {
    8.                 collectMessage = null;
    9.                 collectedMessage = GUI.TextField(new Rect(10, 10, 200, 20), collectedMessage, 25);
    10.                 StartCoroutine ("MessageDisappear");
    11.                 if(removeTextBox == true)
    12.                 {
    13.                     Debug.Log ("the coroutine worked");
    14.                 }
    15.             }
    16.         }
    17.     }
    18.  
    19.     IEnumerator MessageDisappear()
    20.     {
    21.         yield return new WaitForSeconds (3);
    22.         removeTextBox = true;
    23.     }
    As you can see, everything functions just fine, but I am having trouble removing the textfields I create. I try setting the collectMessage to null to remove it, but that fires a NullReferenceException. How would I remove the field so it doesn't show?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    Set it to an empty string like so:

    Code (csharp):
    1. collectMessage = "";
    Not sure if that is the behavior you WANT, but it won't throw nullrefs from collectMessage...