Static Function, Odd Error?

I have this code, which is on objectA in scriptA. It wants to call a static function on objectB, scriptB.

///enemyScript
function score()
{
	scoreMasterScript.runningScore += thisScoreValue;
	scoreMasterScript.adjustScore();
}

However I get this message error, that “Assets/Scripts/scoreMasterScript.js(15,9): BCE0020: An instance of type ‘scoreMasterScript’ is required to access non static member ‘scoreText’. An instance of scoreMasterScript does exist in the project (not hierarchy at the moment).

This is my code for the static function in scriptB (titled “scoreMasterScript”)

//scoreMasterScript
static var runningScore : float;
var scoreText : TextMesh;

static function adjustScore()
{
	///score set in delegate
	scoreText.text = runningScore.ToString();
}

Why is that an error? What do I need to do? My script name is correct “scoreMasterScript” So I do not understand why there would be an error?

scoreText needs to be static as well.

Why is that? Why must any element in a static function must itself be static?

Also in order to assign the static text var, I needed to create non static text var, then just pass it off at start. Is this the only way I can assign the static text var? Why do static vars not show up in the inspector?

I am not 100% sure why you can’t use non static variables in a static function but you can use static variables in a non static function. I am not great at js but what you could do is this

//scoreMasterScript
static var runningScore : float = 0;
var scoreText : TextMesh;
static var s_scoreText : TextMesh;

function Start()
{
    s_scoreText = scoreText;
}

function Update()
{
    scoreText = s_scoreText;
}

static function adjustScore()
{
    ///score set in delegate
    s_scoreText.text = runningScore.ToString();
}

That should get rid of your error and you be able to still use the inspector to assign the text mesh.

That’s what I did.

Seems odd tho that a) a static var does not show up in the inspector and b) a static function can only use static vars inside of it. If anyone has a logical explanation I would love to hear it.

Thanks.

Edit. Double post.

Hi renman,

static means ‘associate’ with the class itself, not an object of the class. So if static function tried to change a non-static variable, it wouldn’t know which object that variable belonged to. Here’s an example:

public class mySimpleClass
{
	public int someValue;
	
	//a static function trying to access a non-static variable, won't compile
	//but let's pretend like it will
	static int GetValue()
	{
		return someValue;
	}
}

//make an object of mySimpleClass
mySimpleClass object1 = new mySimpleClass();
object1.someValue = 10;

//make another object of mySimpleClass
mySimpleClass object2 = new mySimpleClass();
object2.someValue = 20;

//now call Getvalue
//What should be printed here, 10 or 20?  AHHHHHH ERRRRORRR
print(MySimpleClass.GetValue());

Oh I see.
But why would the values not be able to be seen in the inspector?

For the same reason. By design the Inspector is instance-based.

So is the only way, or at least a good way, to assign any var in my masterScoreScript, full of static elements, to create an equivalent type that is non static?

static var textObj : textMesh;
var textObjNonStatic : textMesh;

function Start()
{
textObj = textObjNonStatic;
}

That, or you could apply a script to your textmesh which continously updates the mesh or use a static event updateScore that your script on the textmesh listens to.

Or do a GameObject.Find on scene setup, or the first time the score gets updated.

Awesome.
Thanks guys.

This method opens your static member up to mutation if you attach that script to more than one GameObject.