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 MrDizzle26 · Mar 01, 2015 at 08:01 PM · statictypecast

Cast Static variables as generic ones?

I am making a game with upgrades, I have class call "GlobalStats" which contains the static variables I need to reference.

I have made another class "MenuUpgrade" that needs to reference these variables, but I want to define which variables are referenced in the editor and not in code, this is so I don't have to duplicate everything 50+ times, and hence the class has to be use generic variables without references to particular static variables.

How can I cast a generic variable in my non-static class as a static variable?

public class GlobalStats : MonoBehaviour
{

 // Static Variable

 public static int maxSpeedLevel = 0;
 public static float playerMaxSpeed = 12.5f;

}

public class MenuUpgrade : MonoBehaviour
{

 // Generic Variables

 public string upgradeLevel;
 public string upgradeStat;

}

Comment
Add comment · Show 3
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 sparkzbarca · Mar 01, 2015 at 08:17 PM 0
Share

often times especially if your newer it's better to say what your trying to accomplish broadly ins$$anonymous$$d of what you have now for example.

i'm not even entirely sure what your trying to do but and i'm not trying to rude, i'm almost sure what you think is the correct way to solve the problem through static variables and such is simply the wrong way from a simplicity point of view to solve it.

if for example you say I want when I increase upgrade level for it to increase a bunch of different variables but i'm not sure which and not sure by how much right now so i'd like that to be dynamic or i'd like to define that in the editor. (is this what you want?)

that is your problem, you might suggest how you think you'd do it but I wouldn't settle on your solution as the definitive best one and ins$$anonymous$$d describe what behavior you want to end up with and then ask for input on how to achieve it.

avatar image DiegoSLTS · Mar 01, 2015 at 08:56 PM 0
Share

I'm not sure what you're asking, generic and static variables are 2 unrelated things:

Static variables: https://msdn.microsoft.com/en-us//library/98f28cdx.aspx

Generics: https://msdn.microsoft.com/en-us/library/512aeb7t.aspx

Then, "cast" also means something not related to your question: https://msdn.microsoft.com/en-us/library/ms173105.aspx

You can cast something to change it's "static" nature, you cast the value.

You're using the wrong words to make your question, it makes no sense as it is right now, try to use the actual words for the things you have and the things you want to do.

avatar image MrDizzle26 · Mar 01, 2015 at 10:49 PM 0
Share

Sorry for the poorly phrased question, but I did find a solution, although it was more of a hack than the original solution I intended to find.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MrDizzle26 · Mar 01, 2015 at 11:54 PM

To get around this problem I created new variables in which to store the value of the static ones.

 int iUpgradeLevel = 0; 
 float iUpgradeStat = 0; 
 int iUpgradePrice = 0;

Rather than try and reference the "GlobalStats" static variables as strings in my code, I entered a boolean check so that I could toggle in the inspector which set of statics to reference.

 public bool bMaxSpeed;
 public bool bAcceleration;
 public bool bBoostSpeed;

Depending on which bool is checked determines when values are passed into my upgrade menu slot, and which values will be altered by the function.

 if(bMaxSpeed == true)
 {
     iUpgradeLevel = GlobalStats.maxSpeedLevel;
     iUpgradeStat = GlobalStats.maxSpeed;
     iUpgradePrice = GlobalStats.maxSpeedPrice;
 }
 
 if(bAcceleration == true)
 {
     iUpgradeLevel = GlobalStats.accelerationLevel;
     iUpgradeStat = GlobalStats.acceleration;
     iUpgradePrice = GlobalStats.accelerationPrice;
 }
 
 if(bBoostSpeed == true)
 {
     iUpgradeLevel = GlobalStats.boostSpeedLevel;
     iUpgradeStat = GlobalStats.boostSpeed;
     iUpgradePrice = GlobalStats.boostSpeedPrice;
 }

These values can then be used as part of a generic upgrade system.

 // Deny Upgrade!
 if(GlobalStats.cash < iUpgradePrice || iUpgradeLevel == 5)
 {
      audio.PlayOneShot(deny, 1f);
 }
     
 // Buy Upgrade!
 if(GlobalStats.cash >= iUpgradePrice && iUpgradeLevel < 5)
 {
     iUpgradeStat += statAmount;

     GlobalStats.cash -= iUpgradePrice;
     iUpgradePrice *= 2;
     iUpgradeLevel += 1;
     bSwitchSprite = true;
     audio.PlayOneShot(click, 1f);
     audio.PlayOneShot(drill, 1f);
 }
     
 if(iUpgradeLevel < 5)
 {
     currentText.text = currentString + ": " + iUpgradeStat.ToString();
     costText.text = iUpgradePrice.ToString();
 }
     
 if(iUpgradeLevel >= 5)
 {
     costText.text = "MAX";
     currentText.text = currentString + ": " + iUpgradeStat.ToString();
     descriptionText.text = "Fully Upgraded";
 }

Once an upgrade has been purchased I then make sure to change the value of the original static to represent there new values.

 if(bMaxSpeed == true)
 {
     GlobalStats.maxSpeedLevel = iUpgradeLevel;
     GlobalStats.maxSpeed = iUpgradeStat;
     GlobalStats.maxSpeedPrice = iUpgradePrice;
             
     statAmount = 1.25f;
 }
         
 if(bAcceleration == true)
 {
     GlobalStats.accelerationLevel = iUpgradeLevel;
     GlobalStats.acceleration = iUpgradeStat;
     GlobalStats.accelerationPrice = iUpgradePrice;
             
     statAmount = 0.005f;
 }
         
 if(bBoostSpeed == true)
 {
     GlobalStats.boostSpeedLevel = iUpgradeLevel;
     GlobalStats.boostSpeed = iUpgradeStat;
     GlobalStats.boostSpeedPrice = iUpgradePrice;
             
     statAmount = 5;
 }

This provided me with the solution I was looking for, but require me to use these boolean checks rather than pass in the name of the static variable to use. I'm sorry for the poor phrased question but I hope this helps make sense of what I was asking.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

C# dynamic typing 3 Answers

C# Casting Compile Error 1 Answer

Accessing properties from variable 1 Answer

NullReference Problem with pragma strict 1 Answer

Classes and type casting? 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