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 youngapprentice · Aug 08, 2012 at 03:07 AM · gamevariablehard

Add a Prefix to a Variable

Hi, all! This would spare me a HUGE amount of time if this is possible...

Is there any way I can add a prefix to a variable? In my game, I have 3 save slots, and a different variable set for each, and a static int in a master script that holds the current and active slot number. For instance: If I make a variable 'apple', I'd have to make it for all 3 slots, which is fine by me. So, in a script labeled 'Master', there would be:

 static var S1_apple : int;
 static var S2_apple : int;
 static var S3_apple : int;

Which is great. But if I want to modify it in another script, I have to do this:

 var value = 23;
 
 function Start(){
     if(Master.CurrentSlot == 1){
         S1_apple = value;
     }
     if(Master.CurrentSlot == 2){
         S2_apple = value;
     }
     if(Master.CurrentSlot == 3){
         S3_apple = value;
     }else{
         Debug.Log("You forgot a slot!");
     }

}

For EVERY VARIABLE which takes forever and a half. So my question is this:

Can I add prefixes to variable names? At the beginning of the script, I would get the CurrentSlot in the Start() function, and then assign either S1_, S2_, or S3_ to a variable depending on the slot. Then, when I want to modify a variable, I could just use the prefix variable like

 *prefix variable* + apple = value;

Look how much code that saved! 0_o But obviously that doesn't work.

Is there a way to do this? You'd be my saviour! :D

Thanks!- YA

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 Eric5h5 · Aug 08, 2012 at 03:08 AM 1
Share

Why don't you just use an array?

avatar image Muuskii · Aug 08, 2012 at 03:11 AM 0
Share

Why don't you use an arr. . . . Eric beat me Yes, if you use an array your code would look like

apple[$$anonymous$$aster.CurrentSlot] = value;

avatar image youngapprentice · Aug 08, 2012 at 03:14 AM 0
Share

so I should just change the variable names to apple0, apple1, and apple2?

1 Reply

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

Answer by Bunny83 · Aug 08, 2012 at 03:14 AM

How about an array? Are you sure you need those variables to be static? Anyway...

 static var apple : int[] = new int[3]; // array with 3 elements
 
 var value = 23;
 
 function Start()
 {
     // array indices start at 0 not 1
     var index = Master.CurrentSlot-1;
     if(index >= 0 && index < apple.Length)
         apple[index] = value;
 }


Comment
Add comment · Show 10 · 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 youngapprentice · Aug 08, 2012 at 03:25 AM 0
Share

yes they must be static, as the script that modifies them is not local to them. so i would just use these [] to add to a variable name?

avatar image Eric5h5 · Aug 08, 2012 at 03:37 AM 1
Share

> yes they must be static, as the script that modifies them is not local to them.

That's not relevant. Static means there's only one instance of a variable; it has nothing to do with local access. A public variable is accessible from anywhere regardless of whether it's static or not (though the method of access may differ).

> so i would just use these [] to add to a variable name?

At the risk of sounding snarky, I think you'd be doing yourself a huge favor if you took some time out to do some basic computer language learning. Take some classes, or find something online at least. Arrays are a really fundamental concept that you must know if you're going to do any program$$anonymous$$g. Otherwise you're going to continue wasting a lot of time trying to find "workarounds" for things that don't need to be worked around.

avatar image Bunny83 · Aug 08, 2012 at 03:38 AM 0
Share

No, you can't add something to a variable name. An array is a collection of multiple values of the same type. In the index brackets [] you have to put the index which is always an integer value between 0 and count-1.

In my example the array contains 3 elements so valid indices are 0, 1 or 2

btw: The fact that another script / class have to access this variable doesn't require you to make it static. Take a look at the Accessing_Other_Game_Objects documentation page.

avatar image youngapprentice · Aug 08, 2012 at 03:48 AM 0
Share

Haha oh my gosh thank you Bunny. Lol I do have a large amount of coding experience. Did I come off as not knowing what I was talking about? I realize that he was indexing the array. Haha I was just hoping that I could add to it. yes I know arrays start at 0. this is a great solution to my problem except for one thing: The variables I am trying to access already belong to arrays in their respective Slot numbers so it is eaey for me to save data... Is their another 'workaround'? :P

avatar image Eric5h5 · Aug 08, 2012 at 04:46 AM 1
Share

Sure it matters. It's nothing to do with being exposed to the editor; public and static are separate things. It's not like public and private, which are opposites. A variable can be public and static, or public and non-static, or private and static, or private and non-static, plus a couple other access modifiers like protected. As I said, static means there can only be one instance of that variable. (Which leads to people repeatedly asking questions about "how come when I change this variable, it changes on all of my objects!!!11111!11")

Show more comments

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

10 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

Related Questions

Array Problem - Error Code BCE0022 1 Answer

Array Problem - Error Code BCE0022 2 Answers

global variable need advice 1 Answer

What are the sytem requirements? 0 Answers

getting udp package info inside unity (GlovePIE) 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