- Home /
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.
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..
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.
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
}
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.
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 }
Right. And also, OP should read a book about C# basics first.
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.