Basic Unity/C# Return question

So I’m just starting with C# and I’m encountering a problem where two methods are supposed to affect the same variable (Gasfill) but in practice, only one of them seems to work or when I have gotten them both to work, one seems to overwrite it with their own version of the variable (i.e. the subtracting method keeps subtracting when called and the adding method keeps adding but they aren’t adding or subtracting from each other, only on itself from memory). I thought the issue was that I had them as void methods rather than returning which i thought might relink to the variable outside of the methods, but I’m getting a “method return type is incompatible error” now. Any help?
I’ve attatched an image of both the version where it’s overwriting as well as where it’s encountering the return error.

I’m sure this is really obvious, but could really use some help as a beginner

I believe that the left implementation would be a better idea, setting the ValueText every frame is wasteful when it isn’t going to change every single frame. You shouldn’t need to return anything from those methods as GasFill is a public int, it can be read by anything.

But as for the problem, how are you calling those functions? Do you maybe have multiple of those components on a gameobject?

Thank you! Looks like the issue was that I had two seperate Game Objects for the two methods (triggered in game now by two buttons on screen) but by making it only one object called by both buttons to different functions, it seems to work perfectly now!

1 Like