question about script

Okay so I have two scripts, one is EnemyDamageReceiver.js, and I have this bit of code for it.

if (hitPoints <= 0.0) {
			testexp.exp+=15;
			// Start emitting particles
		}

The other script is testexp.js and this is the code it contains.

static var exp : int = 0;
var level : int = 1;
var maxLevel : int = 100;
var expNeeded : int = 150;
var expIncrement : int = 2;
var guiTextExp : GUIText;

function Update(){
	if(exp >= expNeeded){
		expNeeded = expNeeded * expIncrement;
		level++;
	}
	Debug.Log(exp);
	Debug.Log(level);
	guiTextExp.text = "Experience : "+ exp+ " | " + "Experience Needed : " + expNeeded + " | " +"Current Level :" + level;
}

Basically when i kill these enemies, i want it to add 15 on to the int exp. But it doesnt update the amount of exp whenever I kill something. Can anyone see why? Any advice would be very helpful.

How do you set the 'testexp' variable? Why is 'exp' a static? If you add a Debug.Log just before the call to update test expo.exp, does it print?

Yeah prints zero.

Also it is a static because I access it from another class. Guess I forgot to show this bit of code for the first script: var EXP : testexp; var script = GameObject.Find("Player").GetComponent(testexp); That is how I declare the testexp. I assume my basc logic is correct, but its the right track wrong train in this situation.

Static means you have a single variable for ALL instances of that script. If you use the script on multiple objects they will share this variable. Static is NOT how how you access a variable from another script. You get the script from the game objects you're interesting in and access the variable through that. In your case you could do: script.exp; You probably need to make the type of the 'script' variable 'testexp' for this to work. I'm not 100% sure as I don't use Unityscript/JavaScript.

1 Answer

1

You have assigned int value = 0, that too static, this will not update your parameter.

exp = ???

exp needs something to be updated by which is non static.

 if(exp >= expNeeded){
expNeeded = expNeeded * expIncrement;
level++;
}

This code will never execute from what you have shown here. 0 > = 150, is always a false since you never apply any new value to the exp, exp being at 0.

remove static, Update is a non static call.

With my encounters in the unity engine, exp is static because it is being accessed from another class. If I dont make it a static var then I cant use it in the first script. It is obviously SUPPOSED to update the amount of exp in the first script to eventually make exp >= expNeeded true. Guess my logic here is off lol.

@cwaite84: static isn't for accessing from other classes, it means there is only one instance. Use GetComponent to access other scripts.

Like what @Eric5h5 said, you need to get the component which is a script and then access its members.

so itd look something like this? var script : testexp; function Start(){ script = GameObject.Find ("Player").GetComponent(testexp); }