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 Jack62Lewis · Nov 19, 2014 at 07:44 PM · c#variableslogicadding

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);
         }
 
     }

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4

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++;

Comment
Add comment · Show 9 · 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 Jack62Lewis · Nov 19, 2014 at 07:54 PM 1
Share

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.

avatar image Bunny83 · Nov 19, 2014 at 08:01 PM 0
Share

@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?

avatar image Jack62Lewis · Nov 19, 2014 at 08:07 PM 1
Share

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.

avatar image Bunny83 · Nov 19, 2014 at 08:23 PM 0
Share

@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?

avatar image Jack62Lewis · Nov 19, 2014 at 08:37 PM 0
Share

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.

Show more comments
avatar image
1

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;
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
avatar image
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 :)

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
avatar image
0

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.

Comment
Add comment · Show 6 · 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 Jack62Lewis · Nov 19, 2014 at 11:07 PM 0
Share

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.

avatar image Uldeim · Nov 19, 2014 at 11:11 PM 0
Share

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?

avatar image adelphiaUK · Nov 20, 2014 at 10:25 AM 0
Share

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;
         }
     }
 }

avatar image Jack62Lewis · Nov 20, 2014 at 05:24 PM 0
Share

That gives me this error: CS1520: Class, struct, or interface method must have a return type

avatar image Jack62Lewis · Nov 20, 2014 at 05:31 PM 0
Share

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.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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