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 CAPTNCAPS · Dec 13, 2014 at 06:10 PM · errornullreferenceexception

Global Variables & Object reference not set to an instance of an object

Hi... I need your help

I have a script "GameControl" and a script "SecurityCamera".

SecurityCamera checks if the player is seen by the camera and calls alarm. Alarm should be a game ending event and restart the level.

In GameControl I want to store variables and functions used by multiple things.

GameControl Script:

     public class GameControl : MonoBehaviour {
         public static GameControl control;
         public void Respawn() {Code Stuff}
 }

SecurityCamer Script:

 void OnTriggerEnter(Collider other)
     {
         if (!other.CompareTag("Player"))
             return;
         GameControl.control.Respawn();
     }

Now I have the problem that this gives me the error:

NullReferenceException: Object reference not set to an instance of an object EnemyCamera.OnTriggerEnter (UnityEngine.Collider other) (at Assets/EnemyCamera.cs:47)

(double clicking it marks "GameControl.control.Respawn();" in my IDE)

---------------------------------------------------------------------------

I looked it up in my old files, where I once did the same and it worked.

Exactly the same script for "GameControl".

Only difference: how I call it

 void Start () {
 Save.onClick.AddListener(() => { GameControl.control.Save(); });
 Load.onClick.AddListener(() => { GameControl.control.Load(); });
 }

----------------------------------------------------------------------------

So why is this happening now? How can I fix it?

Comment
Add comment
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

2 Replies

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

Answer by SkaredCreations · Dec 13, 2014 at 06:30 PM

If the player is spawned inside the security camera trigger when the level starts, then it's probably because GameControl.control hasn't been yet set (I suppose you're setting it in GameControl.Awake or GameControl.Start).

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 CAPTNCAPS · Dec 13, 2014 at 06:54 PM 0
Share

Player is spawned outside the trigger.

Edit: just found it in my old files... yeah, I forgot to properly set it in Awake >.<

Sry... I feel like I wasted your time now...

avatar image
0

Answer by DRRosen3 · Dec 13, 2014 at 07:40 PM

You don't need the static variable (`public static GameControl control;`) INSIDE the GameControl script itself. Get rid of it.

In your SecurityCamera script you can add a reference to your GameControl script.

 private GameControl gameControlScript = new GameControl():

Then you just call that reference in the code.

 void OnTriggerEnter(Collider other)
      {
          if (!other.CompareTag("Player"))
              return;
          gameControlScript.Respawn();
      }

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

27 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is wrong with my script? 1 Answer

NullReferenceException: Object reference not set to an instance of an object. 2 Answers

NullReferenceException - Why? 2 Answers

Another NullReferenceException: question...(Java) 1 Answer

3d Platform Tutorial Help 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