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 Sendatsu_Yoshimitsu · Sep 07, 2014 at 04:44 AM · c#serializationxml

How do I keep references to unique NPCs between scenes?

My game has a large number of NPCs, currently around 500, and among other things each NPC keeps an Affinity score indicating how much he likes each of the other NPCs. Currently I accomplish this with two lists, one List filled with NPCs, and a List containing the Affinity score. This is fine for a cobbled-together prototype, but gameobjects aren't serializeable, and so I can't save my gameobject list between scenes. I've been flirting with the idea of storing all NPC information in an XML database, so each scene would start by instantiating 500 blank NPC prefabs and then filling out their numbers based on the database, but that still doesn't give me a unique identifier. Right now the best solution I've come up with is giving every NPC an ID number, but every time I needed to adjust someone's Affinity I would have to do a GetComponent for every NPC to check the script that has their ID in it, and if multiple NPCs are changing Affinities at once, it could quickly kneecap my performance.

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
0
Best Answer

Answer by supericecream · Sep 07, 2014 at 05:17 AM

Since its between scenes, you can do with a static class that holds the two lists. Static members are "persistent" while the program is running, and is as fast as C# can make it go.

Alternatively use some optimization magic for your current method.

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 Sendatsu_Yoshimitsu · Sep 07, 2014 at 05:40 AM 0
Share

Static classes are still lost when the program ter$$anonymous$$ates though, right? Ideally I'm trying to work out a solution that will cover both scene changes and save-quit-reloading.

avatar image supericecream · Sep 08, 2014 at 10:41 AM 0
Share

To have the data existing after the program ter$$anonymous$$ate, you obviously would need to save the data to a persistent location, e.g. a save file. However, static classes are easier on performance, so use that while switching scenes. When you need to save the data, just save the data in the static classes to a local file (Presto!).

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

Best practice to store and load a specific set of objects? 3 Answers

XMLDocument GetElementByID returning null 1 Answer

How to control serialized variables over the inspector pane? 1 Answer

How do I get a reference to a gameobject from its GUID? 3 Answers

Making my XML file multiline 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