Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Lorsan · Jul 02, 2015 at 03:52 PM · c#errornull reference exception

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

alt text

space-shooter.png (310.6 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xjoel · Jan 25, 2017 at 08:14 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 !!! alt text


tag-gamecontroller.png (28.7 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges