Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by CrucibleLab · Jul 29, 2015 at 02:43 AM · referenceloadlevelpersistence

Static object losing references in the Inspector after loading

This topic pertains to Mike Geig's Live Training video titled 'Persistence - Saving and Loading Data', which can be found here.

Basically, I'm using a gameObject called "GameManager" to persist game data across multiple scenes and this approach involves the use of what Mike refers in his video as the "Singleton", which is often used in a more broader context within the Object Oriented Programming world.

Anyway, the GameManager script is attached to each Scene and I have several references to other game objects and components via the Inspector panel. Once again, these references are part of the GameManager script.

As for the GameManager script itself, I use the following code block to either keep or destroy the GameManager object accordingly:

 public static GameManager manager;

 void Awake() {
     if (manager == null) {
         DontDestroyOnLoad(gameObject);
         manager = this;
     } else {
         Destroy(gameObject);
     }
 }

When I load each individual scene manually there is no problem whatsoever; meaning all those references I created in the Inspector are intact and work correctly. But, when I load another scene using the Application.LoadLevel(1) method, the specified scene loads just fine, but all the references I created for the GameManager script get dropped.

I did some digging on this topic and came across this post, which appears to be somewhat related but I still can't seem to fully understand how to work around the specific problem I have. Is adding references via the Inspector to the persistent object a bad practice, or am I just doing it wrong?

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
0

Answer by Bunny83 · Jul 29, 2015 at 03:01 AM

You should avoid having the same singleton object in each scene. That means you have multiple instances of that object each serialized into a different scene. Manager objects should never be inside a game scene. If you want to have a manager where you can assign references to, make sure you only load that scene once at start.

Also keep in mind if your singleton references other objects in the scene that are not persistent, those references will become null when you load another scene as all objects that has not been marked with DontDestroyOnLoad will be destroyed.

Placing the same singleton object in each scene makes no sense as your singleton code will get rid of the singleton that is loaded with the new scene since there is already one. However the old one might have lost it's references.

If something persentent needs to access something that gets loaded dynamically you have to use GameObject.Find / FindObjectOfType / GetComponent to get those references.

Another way is a two way approach. Your GameManager is a real singleton which is only loaded once at start. But you simply use another class which is not a singleton but holds all important references in each scene. Your singleton object just has to find that one object in the scene and has access to everything that object references. You would have to place such an object in each scene and make sure you don't mark it with DontDestroyOnLoad. It should be destroyed with the old scene since the references it contained are obsolete.

Here's an example:

 // GameManager.cs
 
 public static GameManager manager;
 public SceneData data;
 
 void Awake() {
     if (manager == null) {
         DontDestroyOnLoad(gameObject);
         manager = this;
     } else {
         Destroy(gameObject);
     }
 }
 
 void OnLevelWasLoaded(int aLevelID)
 {
     // If a new level is loaded, find it's "SceneData" object in the scene
     data = FindObjectOfType<SceneData>();
 }

And this could be the "SceneData" class:

 public class SceneData : MonoBehaviour
 {
     public GameObject player;
     public Camera sceneCamera;
     // ....
 }

Now you can always use:

 GameManager.manager.data.player

as long as every scene has a SceneData object which holds the references to other scene objects.

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 CrucibleLab · Jul 29, 2015 at 01:39 PM 0
Share

@Bunny83 - You probably provided a full, valid answer here (thanks!) but I'm still trying to wrap my head around it. $$anonymous$$ike in his video does attach his singleton object (named GameControl) to each of his two scenes. Are you saying that he could have attached the singleton object to his first scene only and still achieved the same result?

avatar image
0

Answer by ISofK · Oct 04, 2015 at 01:46 PM

@CrucibleLab I'm having the same issue, and the one solution provided doesn't really allow for data persistence! The whole idea is to move data between scenes -not merely to have an object present in all scenes. Having each scene's data in each scene -as stated above- doesn't really solve the problem of moving the score, health, exp, weapons, etc... etc.... between levels. Till someone eventually answer this -if it ever gets answered- I guess PlayerPrefs -or alternatively and probably better- saving/loading provided in the same session of @mikegeig would be a solution. That means every scene will have to load all its info at the start, then save everything before it's destroyed. Much work for the platform to do, and I'm sure there's a better way to do it, but till you can find it...

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

23 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

Related Questions

Have objects persist in one scene, and not across all scenes? (C#) 0 Answers

Referenced script on this behaviour is missing - Strange 0 Answers

Add external DLL reference 1 Answer

What is the Additive Reference Pose checkbox for? 1 Answer

How do I reference the cameraEye in another script 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