What is "variable not assigned"?

public class BlobCollection : MonoBehaviour {
public GUIText scoreText;
private int score;

	void Start() { 
		score = 0; 
		UpdateScore();
		}

	//Tells game to add to score if player touches blob.
	void OnTriggerEnter2D (Collider2D other) { 
		if (other.gameObject.tag == "Player") {
			Destroy(gameObject);
			AddScore();
		}
	}

	//Tells score GUI to add to previous score value and then update the display to display the new score.
	public void AddScore() { 
		score ++;
		UpdateScore();
		}

	//Specifies what the GUI text is to display in game.
	void UpdateScore() { 
				scoreText.text = "Blobs Saved: " + score; 
		}
}

In this code, I am trying to get the score to update each time a blob is touched by the player. However, when it runs and player touches a blob, an appears that states “The variable scoreText of ‘BlobCollection’ has not been assigned,” and the score remains at 0. What can I do to resolve this?

You need to link the scoreText in your script to your GUIText in the scene. Drag the GUIText object from the hierarchy to the blob script scoreText public variable.

Can you please provide more information?

  1. On which gameobject this script is attached? In this case I guess you must be attaching it to the blob.

  2. In this script, the scoreText is not been assigned as script doesn’t know which object is scoreText. Are you publically refering the scoreText through inspector?

  3. Try using adding this line

    scoreText = GameObject.Find( “Your GUIText object name” ).GetComponent();

    in the start function of your script. (Make sure your GUIText object is active in the hierarchy)

public class BlobCollection : MonoBehaviour {
public GUIText scoreText;
private int score;

void Start() {
 score = 0; UpdateScore();
 }

void OnTriggerEnter2D (Collider2D other)
 { if (other.CompareTag( "Player")) 
{ Destroy(gameObject); 
score++; } 
}

//Tells score GUI to add to previous score value and then update the display to display the new score.

void OnGUI() {
 GUI.Lable("new Rect (20,20,60,20) , "Blobs Saved: " + score.toString()); }

}
}