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 DTE462 · Jul 26, 2016 at 07:32 AM · instantiate prefabdata storage

Maintaining more than one player character objects between scenes

2 part question stemming from the same problem.

I am converting a game I started in Construct 2 to Unity. There is an overworld scene with a teeny sprite. Then each level goes to a side scrolling scene from the overworld. And before each level you can pick 1 of 4 characters to play as.

First off, I'm not sure the best way to store information tied to each character (health, ammo, etc). Unity seems to store the data on the actual object which would be great if I could just add "DontDestroyOnLoad" and let the same character with the same health and ammo continue from the last level. But I imagine I have to instantiate the selected character at the beginning of each level and let them get destroyed at the end.

The only solution I can think of is make an empty game object for each character. Store all the needed variables on that object. Add "DontDestroyOnLoad" to their scripts. Then before each level, transfer all the data from the empty game object to the instantiated Character Object. Then at the end of the scene, transfer the updated data back to the empty game object (spent ammo and lost health etc). Or is there a simpler way to keep all the data on each character between the scenes they aren't used?

Also I am having a hard time getting general functions happening. Because the characters are all instantiated, it's hard to target other objects. Like the health bar reacting to player taking damage. I end up tagging everything and then use "FindGameObjectWithTag". But by the end of this game there will be hundreds of tags ... is that typical? Seems a bit clunky and hard to maintain.

Any advice on how to approach this problem would be appreciated.

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 DTE462 · Aug 03, 2016 at 02:18 AM 0
Share

Unfortunately after a bit of research this link is the best I can find;

http://t-machine.org/index.php/2015/03/15/some-thoughts-on-loading-scenes-in-unity3d/comment-page-1/

It's a long article that says basically there is no good way to handle the problem (or any other situation using Unity for complex multi scene projects).

Still looking into a solution. I have CharacterSheet script for each of the 4 characters that holds all the variables used for each character. It's a convoluted mess but it seems to work.

1 Reply

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

Answer by fogsight · Aug 08, 2016 at 02:49 PM

@DTE462, You don't have to be confined to Unity's API. You can create your own Class to store all of the player's data and make it static, or instantiate it on some "DontDestroyOnLoad" script object to keep it alive. You can also move objects between scenes: http://docs.unity3d.com/540/Documentation/ScriptReference/SceneManagement.SceneManager.MoveGameObjectToScene.html

To keep track of objects you should use object pools (List or Dictionary etc. depending on usage, containing custom classes with targeting data for extra convenience). Finding objects on the fly is very costly, you should always reference them. Additionally creating dedicated targeting pools like "ObjectsThatInRange" would speed things up. As well as try reusing objects in pools without destroying them for additional performance gains.

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 DTE462 · Aug 08, 2016 at 03:26 PM 0
Share

Great! All this gives me some avenues to research. I appreciate it.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I calculate the "total length" of a series of instantiated gameobjects? 1 Answer

Instantiated prefabs acting different than those dragged onto scene 0 Answers

Storing XML data for multiple users 0 Answers

What is the best way to create and store levels in Android game. 0 Answers

sqlite encryption question 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