Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Christian.Tucker · Feb 15, 2014 at 01:07 AM · c#save scene

Passing Data Through Scenes?

I'm working on a game that has a scene for the game, and a scene for the Game-Over menu, in the Game-Over menu it displays your score, and asks if you want to play again.

So, I keep the score from the player and move it to the next scene by having the same game-object in both scenes and using

     void Awake()
     {
         DontDestroyOnLoad (this);
     }


However, this is causing a problem where after switching back and forth through the scenes, there are multiple instances of this GameObject in both scenes.. (Because it doesn't destroy to keep that data) but this starts causing major problems.

How can I just keep ONE game object constant throughout multiple scenes?

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 AlucardJay · Feb 15, 2014 at 02:36 AM 0
Share

You are trying to use a singleton approach, but only inmplementing half the singleton, where you have DontDestryOnLoad, but then no check to see if the object already exists in the scene. Search Unity Singleton for many examples here. And as described in the given answer. Also : http://clearcutgames.net/home/?p=437

1 Reply

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

Answer by Demigiant · Feb 15, 2014 at 01:57 AM

You don't really need a GameObject just to keep a score. You could simply use static classes that don't derive from MonoBehaviour.

Still, to keep a single instance of a GameObject, in your case you could add a static variable "instance" to your GameObject. During Awake, you check if instance is null. If it is, you reference the GameObject itself there, and go on. If not null, the GameObject destroys itself instead, since it means there's already another one.

 static GameObject instance;
 
 void Awake()
 {
   if (instance == null) {
     instance = this.gameObject;
     DontDestroyOnLoad(this.gameObject);
   } else {
     Destroy(this.gameObject);
   }
 }
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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

save scene and load using binary file 1 Answer

Admob ads work but are on all scenes 4 Answers

Making a bubble level (not a game but work tool) 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