public int changes to 0?!

Hey everyone,
I am currently having a very irritating issue.
I am in the midst of creating a clicking/idle game and I have a script which has some functions and variables. One of these variables - being the cost seems to change to 0 when I run the function to buy something.
As I have about 5 scripts which are - I feel too long to post here and I cannot seem to pinpoint it so a single/ a couple of scripts I will post the entire project.
All/any help would be greatly appreciated.

In the project currently, I have a scrollview and content etc and within there I have a single button which I am using to test and diagnose this issue.

(The Rar is uploaded to Zippyshare for sheer simplicity as I do not have to sign in - if this is an issue just let me know)
Project:
http://www108.zippyshare.com/v/n8O52kJt/file.html

This is what attaching the debugger is for, put a breakpoint anywhere the variable is modified, and watch for when it’s modified to be 0… you’ll know who called it then.

This is also a place where encapsulation comes in use for debugging, public fields allow your code to modify the value anywhere. Encapsulated fields control who has access, and you can put the breakpoint in one place (as well as deal with strange incoming values for the field… like setting it 0 when it shouldn’t).

And no, most of us won’t download your entire project off of zippyshare (I don’t even know what zippyshare is… and the unity website has a built in uploader to share files with… nevermind who the heck wants to open your entire project for this problem!? We’re not here to do all your work for you, we’re here to offer help and guidance).

2 Likes

People are generally not willing to download entire projects to sift through it after a bug.

An easy way to debug how a variable changes is to change it into a property, and log on the set. So, if you have:

public int cost

change it to:

public int _cost;
public int cost { 
    get { return _cost; }
    set { 
              Debug.Log("Set to: " + value);
              _cost = value;
          }
}

It’s often a ton faster than running a debugger.

1 Like

Agreed, I probably would not be having such an issue if I did not set everything to public, it is sheer lack of experience with doing so. I will look further into such things. Also I had Debug.Log’s in place before I removed them so I could check other things.

I appear to of offended you… the unity uploader only allows 4MB. But yes I do understand that no-one would want to download an entire project.

Being frank is not being offended.

3 Likes

Ok thanks I will put this in place now and I will certainly remember to use it in the future.

What is cost before the function runs?

I think in what I’m looking at, it is zero before the function and 0 afterwards.