- Home /
The question is answered, right answer was accepted
How to Access Variable from Another Function (C# Answers Please)
How do you access a variable from another function? It may sound simple (or stupid) but for whatever reason I can't access it's value. I have something like this:
void start() { var variable = 1; } void update() { var var2 = variable; }
I'm using C# and it's giving me an error message saying variable doesn't exist when I'm trying to use it's value. What syntax should I use so it will know to use variable variable?
Answer by benatfantasylabs · Sep 01, 2017 at 05:42 PM
You need to do something like this:
var variable = 1;
void Start() {
variable = 2;
}
void Update() {
variable++;
}
Answer by look001 · Sep 01, 2017 at 05:44 PM
Define your variable globaly by writing it on top of the method header
int variable;
void Start() {
variable = 1;
}
void Update() {
int variable2 = variable;
}
Follow this Question
Related Questions
Can I pass a variable to a function to be assigned in c# 2 Answers
Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer
Referencing functions from other scripts as variables 4 Answers
Change a variable via functions 1 Answer
getting two results when i shouldnt be 2 Answers