- Home /
Object reference not set to an instance of an object
Hello, I'm pretty new to Unity and Java programming and I've been trying to make a score system.
I have a script called ScoreController and it looks like this :
private var textScore : GUIText;
public var Score : int;
var Amount : int;
function Awake()
{
textScore = GameObject.Find("TextScore").GetComponent(GUIText);
}
function Update()
{
textScore.text = "Score: " + Score.ToString();
}
function AddScore()
{
Score++;
}
function SubtractScore()
{
Score--;
}
function SetScore(Amount)
{
Score = Amount;
}
And I'm using enemy object with script CountdownTimer :
var scoreController : ScoreController;
public var allowedTime : int = 5;
private var currentTime = allowedTime;
function Awake()
{
TimerTick();
}
function TimerTick()
{
while(currentTime > 0)
{
yield WaitForSeconds(1);
currentTime--;
}
scoreController.GetComponent(ScoreController).SubtractScore();
Destroy (this.gameObject);
}
I want it to wait 5 seconds, subtract score and destroy itself. I also have other script that I found on google. It instantiates objects randomly so they all become clones of a prefab.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var soundSpawn : AudioClip;
var amountEnemies = 20; // Total number of enemies to spawn.
var yieldTimeMin = 0.5; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 1.5; // Don't exceed this amount of time between spawning enemies randomly.
var i = 5;
function Start()
{
Spawn();
}
function Spawn()
{
for (i=0; i<10; i++) // How many enemies to instantiate total.
{
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
audio.PlayOneShot(soundSpawn);
Instantiate(obj, pos.position, Quaternion.Euler(75, 0, 0));
i=0;
}
}
But whenever I run the game and 5 seconds pass I get the following error and the object doesn't destroy itself
Object reference not set to an instance of an object
This error happens because of this line:
scoreController.GetComponent(ScoreController).SubtractScore();
I've been looking for how to correct it all over the place, however I was unable to solve it myself so I'm asking help from you guys. Please help.
Answer by KiraSensei · Jun 17, 2013 at 01:16 PM
In the enemy script, you define scoreController as of type ScoreController, but in line 26 you search the component ScoreController. This won't work.
You need to define scoreController as a game object, and in the inspector, put your game object that contains the first script into this variable (it is by default declared public, so there will be no problem).
Thank you for your answer. Now the problem is I've made this enemy a prefab and I can't select a game object for scoreController variable unless I add the enemy to the scene.. Otherwise it just doesn't allow me to choose anything.. These enemies get spawned by other game object when the game starts, so there are none of them placed manually in the scene. Any tips?
Sure, you can find your game object that contains the ScoreController script.
scoreController = GameObject.Find("your game object name");