- Home /
Int Variable++ isn't adding anything.
I'm trying to increase the size of my int variable by one in a function using the following code. Then debugging the amount to see if it has worked. However it seems to not add anything it always says 0 in the console.
public void AddItem(string name)
{
if(name == "Pickup_Apple")
{
apple = apple++;
Debug.Log("You have " + apple + " " + name);
}
}
Answer by Jeff-Kesselman · Nov 19, 2014 at 07:47 PM
Heh. Good bug.
The ++ operator is POST INCREMENT.
This means that if a is 9 then the value of a++ is 9, with 10 stored back into a after the value has been determined.
SO a = a++ is doign the follwing in sequence:
(1) Getting the current value of a
(2) incrementing the value of a
(3) storing the OLD value of a that was fetched before the increment back into a, undoing the increment.
If you want to increment a you want EITHER
a += 1;
OR just
a++;
Hello, Thanks for your reply. However my problem is persisting. With both your suggestions tested I always get 1 as my value. No matter how many times the function is ran.
@jack-l62: I've fixed the type o in the first suggestion. However, the second should work. Where is the variable stored? Is it a member variable of the class? Where is this class used / attached to? From where do you call your AddItem method?
Here's the full script.
public class Inventory_$$anonymous$$ain : $$anonymous$$onoBehaviour {
public int apple = 0;
public void AddItem(string name) // A function called by
{
if(name == "Pickup_Apple")
{
apple += 1;
Debug.Log("You have " + apple + " " + name + "s.");
}
}
}
I call the function from another script in a function of it's own that is called each time I pick up the apple item. That script is inheriting from the one that calls the problem function in the original question.
@jack That script just works fine, if there's still a problem it's most likely related to the way you call the method. Are you sure you actually access the right instance of that script? I guess this script is attached to the player or a manager GameObject, right?
This script is on a manager gameobject. The script that calls the function is on another object that is inheriting from the manger objects script.
Answer by adelphiaUK · Nov 19, 2014 at 08:22 PM
Not quite sure what you are trying to accomplish here but that code is very confusing. For a start, using "name" as a string variable is bad given that Unity uses "name" internally.
I would suggest you change it to more like:
public void AddItem(string itemName) {
if(itemName.Equals("Pickup_Apple")) {
apple++;
Debug.Log("You have " + apple + " " + itemName);
}
}
Hopefully that will work for you. I presume you have declared an initial value for "apple" somewhere in your code i.e:
int apple = 0;
Answer by aled96 · Nov 19, 2014 at 07:58 PM
Use just " apple++; " it should work it is better. Or you can try ++apple; beacuse can happen that apple = apple++; firslty apple is copied then added but not copied; Instead ++apple, firstly add then copy..
Sorry for my english, i hope that you understand what i mean :)
Answer by Uldeim · Nov 19, 2014 at 10:55 PM
From the comments in Jeff's answer, I think I understand the problem.
What's happening is that your Pickup scripts have AddItem(), correct? You call AddItem() from the Pickup script, and expect it to call the Inventory manager one. But it does not! Your AddItem() call is just being run on the Pickup script, and they each have their own Apple count, always making the value 1 (since it starts at 0 and only gets incremented once).
You need to give the Pickup objects a reference to the manager object (either make the Manager a global/singleton or assign it somehow during the creation of the Pickup object) and call it from there. This is not how inheritance works.
I'll look into this. However, there is no apple variable at play at all in the pickup script. The script does appear to working as the debug line is definitely being run. I'm using AddItem(name) to pass a string into the function which the manager/inventory script uses to deter$$anonymous$$e which variable to increment; in this case, the apple. Hence the name/string/if comparison.
If the Pickup script inherits from the Inventory_$$anonymous$$ain script (which you mentioned that it did) then there is, in fact, an apple variable, which has been inherited.
Anyways, I obviously can't really disagree with you since I can't see the Pickup code. Could you Edit your original question and put it up?
Not trying to write your code for you, but if I'm understanding what you are trying to do then you could combine multiple functions into one.
public const int pickup = 0;
public const int drop = 1;
public static int apples = 0;
public static int bananas = 0;
public static int pears = 0;
public void AddItem(string itemName) {
doAction(pickup,itemName);
}
public void RemoveItem(string itemName) {
doAction(drop,itemName);
}
public doAction(int mode, string itemName) {
switch(mode) {
case pickup:
switch(itemName) {
case "Apple":
apples++;
Debug.Log("You now have " + apples + " apples");
break;
case "Banana":
bananas++;
Debug.Log("You now have " + bananas + " bananas");
break;
case "Pear":
pears++;
Debug.Log("You now have " + pears + " pears");
break;
}
break;
case drop:
switch(itemName) {
case "Apple":
if(apples > 0) {
apples--;
Debug.Log("You now have " + apples + " apples");
}
break;
case "Banana":
if(bananas > 0) {
bananas--;
Debug.Log("You now have " + bananas + " bananas");
}
break;
case "Pear":
if(pears > 0) {
pears--;
Debug.Log("You now have " + pears + " pears");
}
break;
}
break;
}
}
}
That gives me this error: CS1520: Class, struct, or interface method must have a return type
Wait never $$anonymous$$d; The code I posted originally works now, after I changed the apple int variable to a static int. Thanks :)
P.S The function in my pickupscript is actually one that comes from RFPS on the asset store. $$anonymous$$aybe that package stores it's own apple variable? I did change the name to check it and it still didn't work.
Your answer
Follow this Question
Related Questions
Inspector Assigned Variables Not In Both Scenes 1 Answer
Passing variables to an instantiated object 1 Answer
C# adding ints 1 Answer
What technique/code is needed for contra style aiming? 1 Answer
How do I add two variables together? 1 Answer