Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BlackSunPhoenix · Dec 08, 2019 at 08:28 PM · variablechangeif-statementsname

change variable name inside IF statement, or add text to it

How can i do the following:

 public string item_slot1 = 0;
 public int current_slot = "1";
 if (item_slot"current_slot" == 0){
     Debug.Log("Success");
 }

I have 25 inventory slots in my game. I need to do some kind of loop to check each slot. Maybe what i'm trying to achieve is not possible in the way i code it. The current_slot contains a text that will be added to the variable name that is used in the IF statement. I wasted like 2 hours trying to achieve that. Nothing worked. Can someone suggest me a way to do it? I dont want to do if statement for each slot, i simply want to loop it.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Fariborzzn · Dec 08, 2019 at 10:09 PM

What Do You Mean By Changing Variable Name?! Looping Is Done Through Collection,Diq,List,Array ... I Dont Get it..

Comment
Add comment · Show 4 · 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 BlackSunPhoenix · Dec 08, 2019 at 10:47 PM 0
Share

Sorry, i was program$$anonymous$$g all day and i got so tried that i couldn't focus on my question to make it as clear as possible. This is how my loop works.

  public int item_slot1 = 0;
  public int item_slot2 = 0;
  public int item_slot3 = 0;
  public int item_slot4 = 0;
  public int item_slot5 = 0;
 
  public int current_slot = "1"; // Gets increased by 1 each time the while runs
  if (item_slot"current_slot" == 0){
      while(current_slot != "6"){
      current_slot = (Convert.ToInt32(current_slot) + 1).ToString();
      }
  }

I only search for a way to make the variable Dynamic inside the IF statement. And change the last number of the name each time the loop runs. It is that simple. Google did not help me. $$anonymous$$y head exploded today... i wasted all day on something i could've done in 30 $$anonymous$$utes.

Edit: If you can suggest a better way for me to do this please reply. Thanks for the answer.

avatar image Fariborzzn BlackSunPhoenix · Dec 09, 2019 at 10:20 AM 1
Share

Hey @imageBlackSunPhoenix as @Cornelis-de-Jager "rather than answering what you are trying to do, try to explain why you are doing it" in this way more answer will find for your question..

i can suggest you ins$$anonymous$$d of having 25 item slot like this: public int item_slot1 = 0; public int item_slot2 = 0; public int item_slot3 = 0; public int item_slot4 = 0; public int item_slot5 = 0; . . . Try To declare an array, you can use the following syntax:

 datatype[] arrayName=datatype[size];

so in your case

  int[] item=new int[25];

you can loop through it and do a same task for every item in your array

 foreach(int in arrayName)
 {
   /do what ever you want
 }

avatar image BlackSunPhoenix Fariborzzn · Dec 09, 2019 at 05:34 PM 0
Share

Thank you so much. And yeah im a complete beginner with c#. I mostly do PHP and there i can do everything i want with no problem. C# seems to be a little bit restricted. But that's okay i will get used to it. Thank you for your time and the nice answer.

Show more comments
avatar image
1

Answer by tejasg · Dec 09, 2019 at 06:07 AM

It seems like a for loop should fulfill your needs, just loop 25 times like: for (int i = 1; i < 26; i++) { #do whatever you need to do at each slot here }

Comment
Add comment · Show 1 · 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 Ermiq · Dec 09, 2019 at 06:42 AM 1
Share

Right. And also, OP should read a book about C# basics first.

avatar image
1

Answer by Cornelis-de-Jager · Dec 09, 2019 at 03:01 AM

Hey @BlackSunPhoenix, even after what you tried to explain more in detail I'm not sure people quite understand what you are trying to do. But rather than answering what you are trying to do, try to explain why you are doing it. For example: "I am trying make it so that X item and only go into X slots or that Y has... blah blah" - that way we know the overall effect, then you can explain more like you did about how you went about it.

You'll find if people can invasion the end goal, its much easier to know what effect you are trying to achieve - and at that point - it is 100% possible.

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

125 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 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 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 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 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

Declaring animations on a script in the editor 1 Answer

How to adjust the speed of an object? 1 Answer

Changing variables in another script 3 Answers

Access and Change variables from other Scripts? 2 Answers

String as variable name, reflection or dictionary or other 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