- Home /
Despawner script returns null reference exception.
I'm currenly developing a 2D game in which you're to avoid some blocks which falls down. Do have them despawn after they leave the screen, I've created a cube that despawns anything that touches is, and that used to work perfectly.
That is until I added a scorepoint system. Basically, I want it to at 10 points to the score everytime a block despawns, as in everytime you dodge a cube, and that worked, but if you did hit a cube and died it just kept on counting. So I made a script which should check if you're alive or not, and stop the count of cubes if you're not, but I get a null reference exception when a cube hits my despawner. Heres my despawner script and my player movement script.
PlayerMovementScript
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
public float Speed = 2.0f;
public int Lives = 3;
public GameObject Player;
public bool CheckNumberOfShipsAlive = false;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.D))
{
transform.position += transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.R))
{
if (CheckNumberOfShipsAlive = true)
{
Debug.Log("Respawn!");
GameObject spawnPlayer = (GameObject)Instantiate (Player);
CheckNumberOfShipsAlive = false;
}
else
{
return;
}
}
}
void OnTriggerEnter()
{
Destroy(this.gameObject);
Lives -= 1;
CheckNumberOfShipsAlive = true;
}
}
DespawnerScript
using UnityEngine;
using System.Collections;
public class DespawnerScript : MonoBehaviour {
public float Score = 0.0f;
void OnTriggerEnter(Collider other)
{
GameObject score = GameObject.Find("Player");
PlayerMovementScript playerMovementScript;
playerMovementScript = GetComponent<PlayerMovementScript>();
if (playerMovementScript.CheckNumberOfShipsAlive = false)
{
Destroy(other.gameObject);
Score += 10.0f;
Debug.Log("Your score is " + Score);
}
else
{
return;
}
}
}
Oh sorry, it's on line 12 in the DespawnerScript :)
Should be "if (player$$anonymous$$ovementScript.CheckNumberOfShipsAlive == false)"
Although that probably isn't causing the error.
In most languages if( variable=something) just sets the variable to something and checks if variable is true.
To get that error though player$$anonymous$$ovementScript is probably null. Try "Debug.Log(player$$anonymous$$ovementScript );" after "player$$anonymous$$ovementScript = GetComponent();"
All of the above, plus judging from your DespawnerScript and it's use of GetComponent, you have a Player$$anonymous$$ovementScript attached to the same object; both react to OnTriggerEnter. Since the Player$$anonymous$$ovementScript reacts to this event by destroying its GameObject (and therefore itself), it seems fair to imagine that nothing good can come of this.
On the other hand, if that GetComponent is written in error then you should expect the return value to be null and to see the exception you describe.
Your answer
Follow this Question
Related Questions
NullReferenceException Error 6 Answers
Instantiate() as GameObject = null reference 1 Answer
Null Reference Exception error 3 Answers