Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 abc24 · May 07, 2018 at 10:04 AM · gameobjectscenestaticload scene

Reloading Scene Causing Null Refference

Hello guys, i m making a FPS game. but whenever i load the scene 2nd time, i get alot of Null References. I know it has something to do with static variables used in 1 or 2 script files or game objects that do not destroy. But, my project has alot of files and somehow i cannot seem to understand the reason behind this issue.

If you want to go to the problem state here are the steps.

  1. Start The Game

  2. Press Escape

  3. Press Yes

  4. Press Start

  5. Look into console Window (You Will See : MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.)

Here is the complete project in Zip Format : https://archive.org/download/abc_TestGameArchive/Test.zip

Kindly download the project and take a look. It will be much appreciated. I am a beginner and still learning things.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tormentoarmagedoom · May 07, 2018 at 10:57 AM

Good day.

If you are receiveng a NullReferenceException, is because some variable is not assigned when trying to access it.

You must track the issue by debuging the code while executing it, discover what variable is null when try to access it,m and then discover why is null.

If you are using DontDestroyOnLoad, make sure the objects are not duplicated, or you are not deleting the object that try to acess..

Bye!

Comment
Add comment · Show 2 · 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 abc24 · May 07, 2018 at 11:01 AM 0
Share

i m not using DontDestroyOnLoad in my scripts. Here is the album that explains the issue : https://imgur.com/a/QwBDVOd and this is my complete Game$$anonymous$$anager Script:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class Game$$anonymous$$anager {

 public event System.Action<Player> OnLocalPlayerJoined;
 private GameObject goo;

 public bool isPaused { get; set; }

 
 private static Game$$anonymous$$anager m_instance;
 public static Game$$anonymous$$anager Instance
 {
     
      get {

          if (m_instance == null) {
              m_instance = new Game$$anonymous$$anager();
              m_instance.goo = new GameObject("_game$$anonymous$$anager");
              m_instance.goo.AddComponent<InputController>();
              m_instance.goo.AddComponent<Timer>();
              m_instance.goo.AddComponent<Respawner>();
          }
          return m_instance;
      }

 }
 private InputController ic;
 public InputController inputController{
 get{
     if (ic == null) {
         ic = goo.GetComponent<InputController>();
         
     }
     return ic;

}

} private Timer m_timer; public Timer Timer { get { if (m_timer == null) { m_timer = goo.GetComponent();

         }
         return m_timer;
     
     }


 
 }



 private EventBus m_EventBus;
 public EventBus EventBus
 {
     get
     {
         if (m_EventBus == null)
         {
             m_EventBus = new EventBus();

         }
         return m_EventBus;

     }
 }
 
 private Respawner m_respawner;
 public Respawner Respawner {
     get {
         if (m_respawner == null) {
             m_respawner = goo.GetComponent<Respawner>();
         }
         return m_respawner;
     }
 
 }
 private Player pl;
 public Player localPlayer {
     get {
         return pl;
     }
     set {
         pl = value;
         if (OnLocalPlayerJoined != null) {
             OnLocalPlayerJoined(pl);
         }
     }
 }

}

avatar image tormentoarmagedoom abc24 · May 08, 2018 at 08:04 AM 0
Share

I tell you the same, some variables are not assigned (oe foir every error you get).

For example in one image i see you get an error for this line:

something = goo.GetComponent();

If get null reference is because:

Or object goo is not defined, you didnt tell Unity what object is goo..

or

goo object does not have a component called Timer.

IF you are sure one this them is not the problem, the problem is the other thing... Explore! Debug the code while executing, and check the states of the variables right before beeing execute! Create Debug.Log with all variables before beeing executed, to see via console whats the state (some of them will be null) !

Bye!!

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

134 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 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 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 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 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

Object not visible in Scene tab until double click it on the Hierarchy. 1 Answer

win game & loos game 0 Answers

How can I detect if a user is looking at a Gameobject inside a scene from a VR headset 0 Answers

Game Objects not using same script independently. 1 Answer

Help me! Create a game object falling from the top scene at click mouse point! T________T 0 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