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 Yarbius · Jun 20, 2014 at 12:40 AM · c#2d-platformerpersistence

Persisting player when loading levels

I am learning unity with a simple learning project, a basic moon patrol clone. Everything was going great until I decided to add some features that require persistence when loading a level, specifically lives and respawning at checkpoints. At first I thought it would be simple, just add DontDestroyOnLoad to the Awake() function but I found that this resulted in an additional player after every death (Oddly stacked on each other vertically). I tried making the class static but I could not because it inherits from MonoBehavior and it wont allow that with an abstract class. I then tried creating a game manager class with just the variables I needed and not inheriting but then I get a warning that the script cannot be loaded because it is abstract. I am thinking I need to have the script not attached to a GameObject but cannot see how.

I have written c# before but the Unity3D way of having a ton of little scripts spread all over make it hard for me to manage the architecture of my code.

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

1 Reply

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

Answer by Kiwasi · Jun 20, 2014 at 01:16 AM

You can use DontDestroyOnLoad and the singleton pattern to ensure you don't get multiple copies of the same gameObject.

You can use static variables. Statics belong to the class and persist between scenes.

You can also create static classes that are not MonoBehaviours.

Then of course there is the tutorial on persistance if you get really stuck.

Without seeing your code its hard to pick exactly what works best.

Comment
Add comment · Show 2 · 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 Yarbius · Jun 20, 2014 at 04:37 AM 0
Share

You would think with all the googling I did I would have found that video. It has a method of doing a singleton that worked great in my case. I need to get used to the idea that in Unity3D a Singleton is not an anti-pattern. Thanks!!!!

avatar image Yarbius · Jun 20, 2014 at 04:40 AM 0
Share

BTW I will up vote this answer once I get enough reputation, I guess rep does not carry over like on most Stack Exchange sites.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers

Flipping a 2D Game object over time 1 Answer

Jumping from special jump orb 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