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 Karsnen_2 · Apr 30, 2013 at 01:13 AM · c#mobileclassstruct

Conversion of a static class to struct?

Hello Devs,

I am right now developing for mobile and was wondering to create a struct which would have certain level information. That information could be used anytime during the gameplay by any other script without creating an instance.

For example, lets say that I want to know the LevelScore then I could just use

 float Percent = LevelInformation.GetCurrentLevelScore()


At the same time, If I want to set the level score - I could

 LevelInformation.SetCurrentLevelScore (1234);

from any script.

With above methodology I hope I am right.

Moving to the next section, I have created a static class. In order use the stack instead of heap I want to convert the static class to a struct. I just need a help from you guys on that. I am blind on that.

 using System.Collections;
 
 public static class LevelInformation 
 {
     private static bool isPAUSED;
     private static bool isITSTARTROOM;
     private static bool isITENDROOM;
     private static bool isLEVELCOMPLETE;
     private static bool isLEVELCOMPLETIONSUCCESS;
     private static bool isHEADSTILLON;
     private    static bool isLEFTLIMBSTILLON;
     private static bool isRIGHTLIMBSTILLON;
     private static bool isMUSICON;
     private static bool isSOUNDEFFECTSON;
     
     
     private static float GameCompletionPercent;
     private static float LevelMetersCovered;
     private static float CurrentMummyVelocity;
     
     private static int ThisLevelScore;
     
     // Initialize during the start of the level.
     public static void InitializeAtTheStartOfLevel()
     {
         
     }
     
     // PAUSE recognition.
     public static bool IsTheLevelPaused()
     {
         return isPAUSED;
     }
     
     public static void SetCurrentLevelScore (int s)
     {
         ThisLevelScore = s;
     }
     
     public static int GetCurrentLevelScore()
     {
         return ThisLevelScore;    
     }
     
 
     
 }
 



Any sort of help is appreciated.

Thank you.

Comment
Add comment · Show 8
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 Eric5h5 · Apr 30, 2013 at 01:25 AM 2
Share

That's not what structs are for. You do want to use a class for this. While there are exceptions, a general rule of thumb is that a struct should be 16 bytes or fewer, and behave "like an int" (in that it would make sense to add them together and so on; for example, a Vector3--which is a struct). The stack/heap thing is irrelevant in this case.

avatar image Karsnen_2 · Apr 30, 2013 at 01:28 AM 0
Share

Thank you Eric. This is the first time I am trying something like this. So is the above class good? I mean is there anything you think I should do concerning my requirement.

avatar image Eric5h5 · Apr 30, 2013 at 01:35 AM 1
Share

It looks like it should function the way you want, though I'd recommend sticking with the Unity convention of always using lowercase for variable names.

avatar image Karsnen_2 · Apr 30, 2013 at 01:45 AM 0
Share

Eric Thank you. And regarding the na$$anonymous$$g convention - doyoumeanlikethis or doYOU$$anonymous$$EANLI$$anonymous$$ETHIS? And could you please enlighten me the benefits on that?

avatar image Eric5h5 · Apr 30, 2013 at 01:49 AM 0
Share

Sorry, I mean the initial letter. It makes things easier to keep straight; for example the difference between transform and Transform.

Show more comments

1 Reply

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

Answer by Eric5h5 · Apr 30, 2013 at 01:57 AM

That's not what structs are for. You do want to use a class for this. While there are exceptions, a general rule of thumb is that a struct should be 16 bytes or fewer, and behave "like an int" (in that it would make sense to add them together and so on; for example, a Vector3--which is a struct). The stack/heap thing is irrelevant in this case.

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

14 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

Related Questions

How -exactly- do classes and structs work in Unity? 0 Answers

How to make classes/structs public to all scripts 0 Answers

C# ArrayList access from other script 1 Answer

cant touch multiple objects on screen (c# , mobile) 0 Answers

All Arrayinstances get changed -1 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