- Home /
Space Shooter: If that impact but not explode or ship or asteroids.
Hello everyone! I am beginner in Unity and I am not a programmer, I do not know C #, as I discovered recently and loved it. I have a problem, I am following the tutorial Space Shooter step. Upon reaching the video: "Counting Points - 14 - Space Shooter ..." everything was fine, however, when shooting, the laser passes through the asteroids, and exploding sounds appear, but not the asteroid and the continuous laser is removed, and of course, it does not score. Also, when the ship collides with an asteroid, explosion sound and if executed, but does not kill the ship. I have reviewed over and over scripts, and are perfect, exactly as in the tutorial. Something must be happening to me. Please if you can help me I would appreciate it. The version is the latest, the 5.1.0f3
Here I leave the mistakes that gives me:
1) Can not find 'Game Controllers' script UnityEngine.Debug: Log (Object) DestroyByContact: Start () (at Assets / Scripts / DestroyByContact.cs: 20)
2) NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets / Scripts / DestroyByContact.cs: 33)
3) NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets / Scripts / DestroyByContact.cs: 35)
And the scripts just in case:
A) DestroyByContact.cs
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.GameOver ();
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy(gameObject);
}
}
B) GameController.cs
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;
public GUIText restartText;
public GUIText gameOverText;
private bool gameOver;
private bool restart;
private int score;
void Start ()
{
gameOver = false;
restart = false;
restartText.text = "";
gameOverText.text = "";
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
void Update ()
{
if (restart)
{
if (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (Application.loadedLevel);
}
}
}
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);
if (gameOver)
{
restartText.text = "Press 'R' for Restart";
restart = true;
break;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
}
public void GameOver ()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
}
C) AND SCREEN
Hi, i know this is an old post but I'm having the same error at the end of video 14, everything was working fine until the part we need to add the way to identify the game controller instance, I double checked and I do have the tag for the game controller object.
NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/DestroyByContact.cs:35)
This is driving me crazy, I can swear i have the same scripts as in the video, hope someone can help me.
Thanks
Answer by Ibzy · Jul 02, 2015 at 03:59 PM
Hi Lorsan,
Welcome to the club! Hopefully you'll get to grips in no time and be developing your dream games :)
First thing I see is from the errors listed is that it cannot find a GameController. This is looking for a specific GameObject in your scene with a Tag of GameController.
If you are following the tutorials, I imagine you have an object called GameController in your hierarchy? If you select this, there is a Tag field at the very top of the inspector view. You may have to add a new tag to have it called GameController, and I believe this is covered within the tutorial?
Answer by Lorsan · Jul 02, 2015 at 08:47 PM
Exactly, the error was that I had deleted the tag of Game Controllers and had not realized, and I was going crazy looking for the error in the scripts. It is already solved and works perfectly.
Thank you very much for the answer and the speed. I'm going to keep having fun with Unity !!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613201634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Null Reference Exception Help 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Sync with visual studio 2010 error 1 Answer
No Monobehaviour scripts in files 1 Answer