- Home /
Narf question on scope - I think :/
ok so I think i'm a bit narf on understanding this.
ive got a IEnumerator with a variable in it. I also have a function that i'm trying to run from the IEnumerator which has the variable in it.
my problem is that i'm getting this error:
error CS0103: The name `theVar' does not exist in the current context
I know I probably should know the answer to this but I don't. can someone explain it in english for me?
cheers
Answer by Alanisaac · Feb 11, 2015 at 02:04 AM
Do you mean you have something like the code below? That code will not compile because the variable name you pass to the function "notTheSameVarName"
is different than the variable name you use in the enumerator "theVar"
. How is MyOtherMethod
supposed to know what theVar
is?
IEnumerator MyMethod()
{
var theVar = new int ();
MyOtherMethod (theVar);
yield return WaitForSeconds (5);
}
void MyOtherMethod(int notTheSameVarName)
{
theVar += 1;
}
I could be totally off here depending on what your code is, so posting your enumerator and method code would probably help if that isn't the case :).
Hi Alfalfasprout,
thanks for posting.
yeah that’s exactly the structure of code I have.
so I guess my next question is, how can I make a variable global so that it can be accessed anywhere? Is it possible?
cheers
You might want to look at some of the basic scripting tutorials on variables and functions for Unity.
If that all makes sense or feels like old news, then skip ahead a few videos to scope and access modifiers, which might help you with some of the variable scope issues you've been having.
The two videos together shouldn't take you much more than 10 $$anonymous$$utes.
ah buddy! I think you’ve answered so many questions for me! Thank you so much for this! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613183016im_/https://answers.unity.com/themes/thub/images/avi.jpg)