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 Jan_Julius · Dec 08, 2014 at 09:24 AM · levelswitchstoring

What is a better way of storing information for levels?

I want to change some variables when entering different levels, what I currently have is this but I bet there is a better way for me to do this;

 switch(level){
         case 1:
             s= 10;
             m= 10;
             break;
         case 2:
             s= 20;
             m= 30;
             break;
 
 etc....
Comment
Add comment · Show 4
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 Redeemer86 · Dec 08, 2014 at 09:37 AM 0
Share

There are many ways to do this... One direct way is to declare the variables as static and then use dontdestroyonload for that object... Or also probably use singleton classes .... Or another way might be playerprefs depending on what is the nature of variables ...

Red

avatar image Jan_Julius · Dec 08, 2014 at 09:45 AM 0
Share

The game runs a level depending on that level the s and m variables change, that's all that's gotta happen but the way I do it now in a script that does a few other things it looks very messy and long so I was wondering if there would be a more efficient or cleaner way, I'll have a look into those 2 and see what I can do, thanks for the reply.

avatar image Redeemer86 · Dec 08, 2014 at 09:53 AM 0
Share

You may also consider, in that case to use ... Application.Isloadedlevel or application.loadedlevel with static variables and dontdestroyonload .... To have variables state depending on level loaded..

Red

avatar image Jan_Julius · Dec 08, 2014 at 11:30 AM 0
Share

This looks very interesting and I've never used it before, I'll go ahead and give it a try, thanks a lot!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Dec 08, 2014 at 09:54 AM

If the level data is as simple as you describe, efficiency wise it doesn't really matter much. I just usually make an array of the data into it's own class in this type of cases.

Something like this: (sorry i didn't write this in Mono so there might be obvious spelling errors in this code)

 public class LevelData
 {
     public int _S, _M;
     public LevelData(int s, int m)
     {
         _S = s;
         _M = m;
     }
 }
 
 public static class Levels
 {
     public static LevelData[] levels;

    // static constructor
    static LevelData()
     {
         levels = new LevelData[]
         {
             new LevelData(10,10),
             new LevelData(20,30),
         }
     }
 
      public static LevelData getLevel(int levelNr)
     {
         return levels[levelNr-1];
     }
 }
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

27 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 avatar image avatar image avatar image avatar image

Related Questions

I need a script for an FPS game. 1 Answer

Does the quality settings remain when you move level? 1 Answer

level up need help 1 Answer

How to add new level? 1 Answer

Loadlevelasync - Application Hangs 2 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