Billamu
September 23, 2010, 6:50am
1
This script is in a prefab. When I run the script, Unity says...
"NullReferenceException: Object reference not set to an instance of an object"
function StartAtARandomSpot( zdepth : float )
{
var campos : Vector3 = Vector3(Random.Range(0,scrWidth), scrHeight+80, zdepth);
campos = Camera.main.ScreenToWorldPoint (campos);
transform.position = campos;
originalSpawnPoint = transform.position;
}
Any help would be greatly appreciated.
system
September 23, 2010, 7:07am
2
Make sure that your camera is tagged as MainCamera, or "Camera.main" won't work. Otherwise, I've verified that your script is fine. Although, you could simplify it to:
function StartAtARandomSpot( zdepth : float )
{
var campos : Vector3 = Vector3(Random.Range(0,scrWidth), scrHeight+80, zdepth);
originalSpawnPoint = transform.position = Camera.main.ScreenToWorldPoint (campos);
}
(Note that you don't even need the campos variable, but for readability you could easily justify keeping it)