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 /
  • Help Room /
avatar image
0
Question by Exytop · Aug 14, 2014 at 02:20 PM · instancenull

NullReferenceException

Hello.

I follow the tutorial for a space shoter from Unity and i have a problem in the final script for counting the score.After i wrote the script i haven't any problems in Console,but when i enter in Play Mod and hit the first asteroid the game crush and give me that error: "NullReferenceException: Object reference not set to an instance of an object". When i double click on the error the program tell's me that is a problem with this line: gameController.AddScore (scoreValue);(Sorry for my bad english -.- )

using UnityEngine; using System.Collections;

public class DestroyByTouch : 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.AddScore (scoreValue);***
     Destroy(other.gameObject);
     Destroy (gameObject);
 }

}

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 MrSteve1 · Aug 14, 2014 at 02:22 PM 0
Share

Is it also printing to the console "Cannot find game controller script"?

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by saschandroid · Oct 20, 2015 at 05:57 AM

I guess it has to be

 void Start () 

(with a capital 'S') and not

 void start()

otherwise the start function is not called at start and you game controller variables are not assigned.

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 xjoel · Jan 26, 2017 at 02:27 AM 0
Share

@saschandroid you saved me man, i was this close to give up and none of the other solutions were working for me, but your answer was spot on, thanks

avatar image
0

Answer by dsada · Aug 14, 2014 at 02:27 PM

Doesn't it logs out that "Cannot find GameController script" ? Because the only possible null value is the gameController variable and you log out this message if it is null.

Most probably you dont have an object on the scene that is tagged with "GameController".

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 MrSteve1 · Aug 14, 2014 at 02:26 PM

It can only be doing this because the game object tagged "Gamecontroller" is not being stored in the variable "gameControllerobject", possibly tag is spelt differently in scene view, or the object does not have the script "GameController" attached to it.

It has to be one of those two options. Check them out.

Hope this helps.

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 $$anonymous$$ · Oct 20, 2015 at 05:07 AM 0
Share

Its the spelling if your doing U5.

if (other.tag != "Player") // not if (other.tag == "Player"){//

     {
         Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
         gameController.GameOver();
     }
     
     gameController.AddScore (scoreValue);
     Destroy (other.gameObject);
     Destroy (gameObject);
 }

}

in short you change only one things, the extra = for a !.

avatar image
0

Answer by Orc_Horn_Productions · Oct 20, 2015 at 10:13 AM

I believe this could be a missleading error. If you've followed the tutorial through it could be that you've forgotten to set your public Text element from GameController in the inspector.

This would explain the NullReferenceException because when DestroyByTouch calls gameController.AddScore (scoreValue) the GameController has no reference to add the score to the UI through.

Check your GameController in the inspector first and if that fails try using the your compilers debugger with a break point on "gameController.AddScore (scoreValue)". That will help prvide more information.

Let us know how you get on.

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 $$anonymous$$ · Oct 20, 2015 at 07:14 PM

OK i give up on space-shooter, I don't know if others have the same issue or not but I made all the corrections, it was starting to work, then the next time I opened it, boom, over half the scripts locked down game play and gave error after error, line after line. The Engine itself tried to change things and screwed it all to hell and back.

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

8 People are following this question.

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

Related Questions

Unity instance missing in "Attach Unity Debugger", Visual Studio 2013. How do I fix this? 6 Answers

Object reference not set to an instance of an object 0 Answers

Damage function affecting all instances of object 1 Answer

Can we anyhow store a prefab in a list when we collide into the instance of that prefab casted as gameObject? 1 Answer

Timer is not counting down. Staying at original number. 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