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 gennadiys · May 23, 2017 at 06:21 AM · errornullreferenceexceptiontutorialspace shooter

Space Shooter - NullReferenceException on Scene Reload

I'd like to get help on understanding the issue I'm having with Space Shooter tutorial. It was completed fully completed and on initial scene load works perfectly. But on restarting the scene the game is loosing number of objects with NullReferenceExpection raised on line

GameObject hazard = hazards[Random.Range(0, hazards.Length)];

Initial Background and Player objects still works, including sounds and Bolt, but following objects are not visible from the scene hierarchy: -Starfield - completely gone -Second Background (scrolling stopping to work) -Game Controller Hazards: Size becomes 0 and references to all Elements are gone.

On full game restart all the missing object reappearing (as per project) and gone again on the scene restart.

The _Complete_Game works perfectly. The scripts are fully compared and all visual issues are checked. Google hints on Awake() and etc. didn't help.

I think it's important to understand the issue before continue exploring Unity. I'd appreciate any idea.

Comment
Add comment · Show 4
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 Ark_Revan · May 23, 2017 at 07:23 AM 0
Share

Does the exact same object disappear each time ? What do you call restarting ? If I remember correctly the tutorial , is it when you press a key like "R" and then you go at the beginning of the level ? If so, can you provide the code of the function ?

I would check that there are no object created in Start() or Destroy on restart but it's hard to say without further information ...

avatar image gennadiys · May 24, 2017 at 12:28 AM 0
Share

Thank you for looking into this. The same objects consistently disappearing after pressing 'R' button. The code of the function:

void Update() { if (restart) { if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.R)) { Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().buildIndex); } } }

Please see screenshots showing which objects are disappearing. Thank you!

[1]: /storage/temp/94641-afterscenerestart.jpg

firstsceneload.jpg (23.8 kB)
afterscenerestart.jpg (38.5 kB)
avatar image TonicMind gennadiys · May 24, 2017 at 06:23 AM 1
Share

I am not entirely sure, but try this:

 GameObject hazard = hazards[Random.Range(0, hazards.Length - 1)]; 

That -1 is important because arrays begin at zero (not one). This method of counting causes the end index to be less one. I ran in to this exact problem recently. Of course then that would raise an array out of bounds exception when trying to get a value from the the last index...

are you sure hazards[] has anything within it? Has it got data or are there any null values inside it? Try a debugger to find out.

avatar image Ark_Revan TonicMind · May 24, 2017 at 07:06 AM 0
Share

I didn't pay attention, but it's entirely possible that this line create an error that stop the loading of the scene in the middle, which explain why half of the object are missing.

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Error after importing assets (Survival Shooter tutorial), Win32Exception 0 Answers

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

NullReferenceException. GetComponent dose not work properly 2 Answers

Why is the boundary not working? 1 Answer

[Closed]NullReferenceException on Object Instantiation onto game world 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