Hey guys! I am pretty new to C# and am currently making my second game using this language. The game is a clone of Breakout and I have been doing really well so far but I am stuck as an error is being shown in the console. After my ball goes into the Trigger zone and tries to re-spawn on the Paddle.
My Death Field Script:
using UnityEngine;
using System.Collections;
public class DeathFieldScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other){
BallController ballController = other.GetComponent<BallController>();
if(ballController){
ballController.Die();
}
}
}
And also my BallController Script:
using UnityEngine;
using System.Collections;
public class BallController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void Die(){
GameObject paddleGameObject = GameObject.Find("paddle");
Destroy(this.gameObject);
PaddleController paddleController = paddleGameObject.GetComponent<PaddleController>();
paddleController.SpawnBall();
}
}
The exact error message is here:
NullReferenceException: Object reference not set to an instance of an object
BallController.Die () (at Assets/Scripts/Breakout/BallController.cs:18)
DeathFieldScript.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Breakout/DeathFieldScript.cs:20)
Thanks to anyone who can help This is my second game using C#