- Home /
Project: Space Shooter - can't assign variable scoreText
Hello, I'm following the Space Shooter tutorial and keep running into a problem. I can't assign a variable to scoreText in the GameController script. First it seems that the tutorial is dated as I'm on Unity 4.60b20 on a Mac Pro and there isn't a GUI Text option under Hierarchy>Create. I created a UI>Text instead and try to drag and drop this into the scoreText field of the GameController script but Unity doesn't let me. This the error I get:
UnassignedReferenceException: The variable scoreText of GameController has not been assigned. You probably need to assign the scoreText variable of the GameController script in the inspector.
Here are the two scripts I'm using:
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
private int score;
void Start ()
{
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
}
And the second:
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour
{
public GameObject explosion;
public GameObject playerExplosion;
public int scoreValue;
private GameController gameController;
void Start ()
{
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
if (gameController == null)
{
Debug.Log ("Cannot find 'GameController' script");
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Boundary")
{
return;
}
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player")
{
Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
Is there are way around this? I'm not a C# programmer and apologize at the noob question. Thank you.
Answer by jazman3d · Oct 08, 2014 at 05:39 AM
Thanks awest, I finally got it working by adding the UnityEngine.UI and using the bit of code you suggested:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour
{
public GameObject hazard;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text scoreText;
private int score;
void Start ()
{
scoreText = GetComponent <Text> ();
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
yield return new WaitForSeconds (waveWait);
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
}
Answer by SetiFX · Jan 09, 2015 at 04:25 PM
These script modification worked for me too, but I had to remove the the same line
**scoreText = GetComponent();** mentioned above by LarryWP
It would be great if the tutorial was updated using the UI
Answer by awest · Oct 08, 2014 at 12:11 AM
The problem is that the GUIText variable will only accept GUIText. You need to change the variable type definition to be that of the text. That's just Text instead of GUIText.
public Text scoreText;
This tutorial on [UI>Text][1] I thought wasn't very helpfull, but near the end is a tiny bit of code for reference. It looks like you may also need this in your Start function.
scoreText = GetComponent<Text>();
Since this is brand new and only in the beta. It may take some finagling. [1]: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text
Answer by LarryWP · Dec 05, 2014 at 11:47 AM
Is your text built under the Canvas object? I did what you did above and I'm able to drop it in the GameController like the tutorial, but when I play, the text does not update and I get an error saying that the text is not set to an object. I'm stuck here now big time. I'm using the 4.6xx version I downloaded today 12-04-2014. I'm at my iPad and can't remember the exact version, but it's newer than your post above. All text now goes under a canvas and I wonder if that's fouling me up.
try removing the line:
scoreText = GetComponent<Text>();
it worked for me.
cheers
Answer by PolarPanda · Mar 29, 2017 at 05:00 PM
I had to add this at the top of my GameController.cs script:
using UnityEngine.UI;
and as suggested by awest, I changed made this change to scoreText:
public Text scoreText;
I'm on Unity 5.6.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity UFO tutorial question about code 0 Answers
Flip over an object (smooth transition) 3 Answers
learn c#? please help. 1 Answer