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 /
avatar image
0
Question by JoshBaz · Jul 27, 2016 at 10:21 PM · variablesscene-loadingscene-switchingnull reference exceptionreloading

Null Reference Exception when reload scene

Hello. I have a really burning question. Whenever i reload a scene or reopen a scene after exiting a another scene I keep getting null reference exceptions on my variables. Basically i am making a racing game so there will be a lot of scene switching. currently I have 5 scenes: 1 splash scene, 1 garage scene and 3 race scenes. The game is programmed to go to the garage, the user picks a race scene, then when the race is is won(or lost or quit) the user presses a button to go back to the garage scene. Whenever i reload the race scene or go back to the garage scene i get the null reference exception.

I don't have any singletons, toolboxes, static classes or static variables. None of my scripts use DontDestroyOnLoad(). Most if not all of my scripts in the scene use Awake() and Start() to set the variables and reference each other at the start of the scene and then they do their business. I've when through about 12 forums and Unity Answers pages and I don't understand anything even though most of them are marked as answered. Some recommend manually setting variables to null and some say to unsubscribe from event handlers, but I don't think I subscribed to any in the first place. I even tried using SceneManager.UnloadScene before using SceneManager.LoadScene. Can someone please help me.

Comment
Add comment · Show 7
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 Nikaas · Jul 27, 2016 at 11:02 PM 0
Share

It could be the loading order of your scripts. For example in a Start method of script1 (lets call it Start1) you may be trying to access variable that have its value set in the Start method of script2 (Start2). But there is no guarantee that Start2 has been already called when Start1 is running. This can be avoided by carefully selecting if something should be placed in Awake or in Start. The last Awake method in your scene is always called before the first Start method. (Or you can use Edit -> Project settings -> Script Execution Order, but I wouldn't use that, because I$$anonymous$$O that just postpones the problem to return as bigger one)

avatar image JoshBaz Nikaas · Jul 27, 2016 at 11:08 PM 0
Share

Hi Nikaas and thanks for your response. But i don't think its the loading order as i have played through the scenes and the others like it using the same scripts and different variables with no errors. Its whem the scene reloads that the error arises as Unity says I'm not referencing an object.

avatar image Denscrivent · Jul 28, 2016 at 03:55 AM 0
Share

You are using unity 5.3? Try using Application.LoadLevel ins$$anonymous$$d.

avatar image JoshBaz Denscrivent · Jul 28, 2016 at 11:54 AM 0
Share

Why should i use Application.LoadLevel ins$$anonymous$$d of the Scene$$anonymous$$anager.LoadScene. What is the difference? Isn't Application.LoadLevel obsolete?

avatar image JoshBaz Denscrivent · Jul 30, 2016 at 02:39 PM 0
Share

I tried using Application.LoadLevel the same error occurs...

avatar image corn · Jul 30, 2016 at 04:58 PM 0
Share

Please post the script(s) in which the errors occur, and the error messages. Without relevant informations, we can only make wild guesses.

avatar image JoshBaz corn · Jul 31, 2016 at 02:20 PM 0
Share

Ok i'll share my scripts hold on.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JoshBaz · Aug 02, 2016 at 08:53 PM

I fixed it. What I did was set references to the variables in the editor before play. And then I also added some code like this:

 Transform example;
 
 void Awake() {
    if(example == null){
       example = GameObject.Find("Example").GetComponent<Transform>();
    }
 }
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

53 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

Related Questions

Variables are resetting after loading the play scene 1 Answer

My game breaks when I load it from another scene (menu scene). 0 Answers

Can I use one script to toggle between scenes? 1 Answer

Scene takes long to load only first time 2 Answers

Can't find game object / script after scene re-enter 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