Hey guys… I get this error when I play my game even though it works fine. But when I build and run I get a crash. If anybody can see some error in my script regarding line 3 I’d love to hear it. Cheers!
The problem is in line 3
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.
I’m trying to alter a variable in another script on another object, are there some other way to do that?
// this is a script on playerReds trigger
public var livesRed : int = 5;
var someScript : s_livesBlue = GetComponent (s_livesBlue);
var heightR : float;
function OnGUI ()
{
GUI.Label (Rect (10, heightR, 100, 20), "Red Lives: " +livesRed);
}
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "playerBlue")
{
livesRed--; //takes life from playerRed
someScript.livesBlue++; //adds life to playerBlue
audio.Play(); //play the audio attached
}
}
function Update()
{
if(livesRed == 0)
{
Application.LoadLevel(0);
}
}