- Home /
The question is answered, right answer was accepted
Why can't I make this VAR public ? (Solved)
I've been using this script from the unity tutorials which works fine as is.
// Print the name of the closest enemy
print(FindClosestResource().name);
// Find the name of the closest enemy
function FindClosestResource () : GameObject
{
// Find all game objects with tag Enemy
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Resource");
var closest : GameObject;
var distance = Mathf.Infinity;
var position = transform.position;
// Iterate through them and find the closest one
for (var go : GameObject in gos)
{
var diff = (go.transform.position - position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance)
{
closest = go;
distance = curDistance;
}
}
return closest;
}
I was trying to make one of the variables public but for some reason it gives me an error. The line/variable is -
var gos : GameObject[];
But when I change that to
public var gos : GameObject[];
I get these error messages-
Assets/AI Scripts/Find Closest Object.js(9,1): BCE0044: expecting }, found 'public'.
Assets/AI Scripts/Find Closest Object.js(29,1): BCE0044: expecting EOF, found '}'.
I don't understand why it suddenly is asking for { when all I've done is tried to change the variable type?
Can somebody explain this please ?
Answer by meat5000 · Jan 29, 2015 at 02:30 PM
When you declare a variable inside a function, it's scope is limited to that function. Declare it outside the function.
I just tried that and (as you already know it works) I moved the line/variable in question to the start of the script and all is fine. Will admit though it seems strange to me that it works like that and not work just because I added the 'public' preface !
Anyway, thanks very much for the reply, much appreciated
:)
Sometimes the simplest explanation is the most appropriate :)
Answer by HarshadK · Jan 29, 2015 at 02:36 PM
The reason behind this is that the variable 'gos' is declared inside the function FindClosestResource() which means that the scope of this variable is limited till the function is executing. Once the function is done executing that variable will be removed hence you can not make it a public variable. Since each time you enter the FindClosestResource() function that variable will be declared again and will be processed inside that function itself and will be removed once the function stops processing. So it is a local variable to that function itself so you can not make it public.
If you really want to make that variable public you can declare it outside that function like:
// Now this is out of function and can be made public
public var gos : GameObject[];
// Find the name of the closest enemy
function FindClosestResource () : GameObject
{
// Find all game objects with tag Enemy
gos = GameObject.FindGameObjectsWithTag("Resource");
Answer by NoseKills · Jan 29, 2015 at 02:37 PM
You can not declare anything public inside functions. If you declare a variable inside a function, it will only be created when you call that function and it will be lost when you exit the function.
It wouldn't make sense to have a variable like that be public so you can access it from other scripts.
If you need a variable to "stay alive", you just have to take it out of the function to make it a "field".