- Home /
Using static constants from an outside script
I'm trying to access static variable in one script with another one, but it's giving me
NullReferenceException: Object reference not set to an instance of an object
Here's the entire script whose static variable I'm trying to access:
#pragma strict
//pragma strict disables dynamic variable typing http://unity3d.com/support/documentation/ScriptReference/index.Performance_Optimization
class Terrain{
public static var CAM_MIN_X:float=0.0;
public static var CAM_MIN_Z:float=0.0;
public static var CAM_MAX_X:float=2000.0;
public static var CAM_MAX_Z:float=2000.0;
}
And here's the block returning the error:
if(Camera.main.transform.position.x<Terrain.CAM_MAX_X && Camera.main.transform.position.x>Terrain.CAM_MIN_X){
Camera.main.transform.position.x+=(oneX*cameraMoveSpeed)*Time.deltaTime;
}
Thanks!
Answer by Berenger · Jun 18, 2012 at 03:36 PM
You can't have a null reference for a float. I'd check if you're camera has the tag MainCamera (that's how Camera.main find it I think).
By the way, a static var isn't constant. It can be read only with the keyword or by declaring only the get though. To have a constant, you need the keyword const. It can still be accessed like a static.
Answer by Cokemonkey11 · Jun 18, 2012 at 07:19 PM
Thank you for the answer, I will make sure to look for that next time.
I didn't know I could use the const keyword in unity.
Cheers.