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 Braindrift · Mar 25, 2016 at 01:39 AM · c#scene-loading

Loading new scene, array looses elements after Start() is executed.

I have a setup where I go back and forth between 2 scenes. Entry is at scene 1, then I load scene 2, then I go back to scene 1, this is where the problem occurs.

In one of my classes, that is used in scene 1, I have a GameObject[] array that I fill with elements in the Start function for the class. For the first time scene 1 is used, everything works fine, but when I come back to the scene the second time, something goes wrong and the array looses it's elements.

     public class board : MonoBehaviour {
         // squares is the array we'r looking at
         private GameObject[] squares;
     
         // Use this for initialization
         void Start () {
             // Using breakpoints, I can confirm that squares is filled up with elements
             squares = GameObject.FindGameObjectsWithTag ("GameBoardSquare");
         }
     
         public Transform GetBrick(int id){
             Transform t = null;
             foreach (GameObject go in squares) {
                 // This is where the code breaks, element in squares have been nulled
                 if (go.transform.GetComponent<boardSquare> ().ID == id) {
                     t = go.transform;
                 }
             }
             //print (5);
             return t;
         }
     }

I've been looking at the execution order of events and can confirm that SceneManager.LoadScene("Scene01") is triggered before the start function in my class. However, the array seems to loose its elements emediaetly after Scene01 is entered and the start function (where I fill up the array) is executed while the game is still in Scene02.

Any pointers, theories or suggested work-arounds are ofc more than welcome. Or ask if there is something I'd need to clarify in the description or code.

Thanks, .E

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 Braindrift · Mar 25, 2016 at 12:54 PM 0
Share

The error message by the way:

$$anonymous$$issingReferenceException: 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.

2 Replies

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

Answer by Braindrift · Mar 25, 2016 at 02:16 PM

Solved,

The problem was not really related to where the code crashed, but related to my EventManager. In my event manager, I have static functions and events that needed to be cleared out before entering the scene a second time. Otherwise they had function references to an instance of an object that was deleted when scene 1 was un-loaded the first time.

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
1

Answer by seth_slax · Mar 25, 2016 at 02:54 AM

Perhaps make the squares array public so you can check to see if it's filled in the inspector on scene load.

Also, at what point is the GetBrick function called? If it's called from another Start() function, it could be that it's being called before this particular Start() is filling it.

You could also try assigning the array in Awake(). It's called before Start() for script initialization.

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 Braindrift · Mar 25, 2016 at 12:51 PM 0
Share

GetBrick is called from an Update() function. Tried using Awake() ins$$anonymous$$d but I get the same result.

Will do some tests with your suggestion on making the array public to see what's going on, see if I can figure out anything from that, but I think I'll just work around the problem and create some initialization methods that are called on the first update made after the scene is loaded, see if that makes it work.

Thanks for input.

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

126 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

Related Questions

How to get scene name at certain buildIndex 5 Answers

Scene Change after some time when achieving a certain Score C# 1 Answer

[SOLVED]My code line don't execute 1 Answer

Change scene by tapping a button in unity 5 UI System 1 Answer

Problem with instantiating objects using playerprefs when switching scenes 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