Resolved need a variable stored outside of functions

Coder8188

New member
Joined
Jun 8, 2020
Messages
2
Programming Experience
Beginner
Yea.
I am working on a game and I need a variable stored outside or functions so I can use it inside others
The problem is when I do it says its not used in the code when it already is and what is even weirder is that I have another one and it works fine!
 
Last edited by a moderator:
Please don't expect us to tell you how to fix code that we can't see. You are obviously not telling us something but you don't know what it is or you wouldn't be asking the question and we don't know what it is because we can't see the code. ALWAYS provide ALL the relevant information, which ALWAYS includes the relevant code. My guess would be that you have declared two or more variables with the same name - one at the class level and one or more at the method level - but we shouldn't have to guess.
 
C#:
        class Foo
        {
            object obj = null; /* Variable at base level */
            void Foo_method_1()
            {
                obj = "my method 1 value"; /* Notice how this variable refers to the obj at the base class level, this is because it has no local variable stored in this method as it does in the one below. */
                obj.ToString();
            }
            void Foo_method_2()
            {
                object obj = "my method 2 value"; /* Notice how obj in this scope does not take on the variable from the base of the class. That's because its redefined at this local level. */
                obj.ToString();
            }
        }
 
Please don't expect us to tell you how to fix code that we can't see. You are obviously not telling us something but you don't know what it is or you wouldn't be asking the question and we don't know what it is because we can't see the code. ALWAYS provide ALL the relevant information, which ALWAYS includes the relevant code. My guess would be that you have declared two or more variables with the same name - one at the class level and one or more at the method level - but we shouldn't have to guess.
Ok I will next time although I did figure it out I accidentlaly declared the variable inside a function while also declaring it at the top
 
Ok I will next time although I did figure it out I accidentlaly declared the variable inside a function while also declaring it at the top
That's what I thought would be the case and @Sheepings clearly did too, but we can never really know for sure unless we see what you've done. In other cases, it might not be so easy to come up with a likely explanation without seeing the code, or we might assume that it's one thing and spend time explaining how to fix that, only to find that it's something else.
 
Back
Top Bottom