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 SebastianRSP · Feb 16, 2014 at 02:08 PM · gameobjectpointgamecontroller

NullReferenceException: Object reference not set to an instance of an object

Hello, I am having problems creating a point system for my 2D space shooter game (I am following the unity tutorial) The problem I am having, is that my game stops when I receive the "points" And then shows me this error: NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/DestroyByContact.cs:30) "

These are my 2 scripts, which should work together:

 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 OnTriggerEnter2D(Collider2D other) {
         Instantiate(explosion, transform.position, transform.rotation);
 
         if(other.tag == "Player"){
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
         }
         gameController.AddScore (scoreValue);
         Destroy(other.gameObject);
         Destroy(gameObject);
     }
     
 
 }
 


 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
 
     public GameObject hazard;
     public Vector2 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
 
     public GUIText scoreText;
     private int score;
 
     void Start () {
         score = 0;
         UpdateScore();
         StartCoroutine (SpawnWaves ());
     }
 
 
     IEnumerator SpawnWaves () {
     
         yield return new WaitForSeconds (startWait);
         while(true){
             for (int i = 0; i < hazardCount; i++){
                 Vector2 spawnPosition = new Vector2(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate(hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds(waveWait);
         }
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
     void UpdateScore()
     {
         scoreText.text = "Score: " + score;
     }
 
 }


Any help is appreciated, thanks!

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 Demigiant · Feb 16, 2014 at 02:10 PM 1
Share

That means that, at line 30, gameController is null.

2 Replies

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

Answer by SebastianRSP · Feb 16, 2014 at 04:38 PM

I found the error, and it was just a small typing fault on my side, line 18 if(gameController = null)

Should have 2 "="

So it would look like this instead: if(gameController == null)

Thanks for the answers :)

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
0

Answer by NoxCaos · Feb 16, 2014 at 04:14 PM

You cann't just call the function of the GameObject. You should use function SendMessage. Just like this:

 gameController.SendMessage("AddScore", scoreValue);

Don't forget to assign your gameController. And you'll have another error in your AddScore function. Change to:

 scoreText.text = "Score: " + score.ToString();


Comment
Add comment · Show 1 · 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 Invertex · Feb 16, 2014 at 02:42 PM 1
Share

Actually, that's incorrect, you can call the function if you have stored the component that contains that function, which is what he did. Using GetComponent in your Start() is much more efficient than calling Send$$anonymous$$essage every time you want to access that. Send$$anonymous$$essage should be used verrrry sparingly.

If you store a script in a variable at Start, you then have access to all it's public functions or variables for as long as the script is alive, and can pass info to those functions as well, without the major performance overhead of Send$$anonymous$$essage or trying to get the component each time.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

setTrigger() for another GameObject doesn't work 1 Answer

How to send a gameobject to a sepcific position in the 3d environment? 3 Answers

At same positioned game objects collision detection 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Rotate a gameobject(player)on the y axis towards mouse position in c# 2 Answers


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