- Home /
getting a variable from another script problem
Alright, so I made a script and attached it to an object, and when ever I shoot the object, it gets the level of the player and multiplies it by 1 (which now that i think about it, thats pointless). any way once health hits zero, the object explodes. This all works. But its also supose to add experience referncing the players script. but it doesn't. I've tried restarting unity and everything. So I'm wondering if theres something wrong with my script. here it is:
var health = 5.0;
var explosion:Transform;
function OnCollisionEnter(hit:Collision){
var level = gameObject.Find("Player").GetComponent(Level);
var playerLevel = level.level;
if(hit.collider.tag == "Fire Ball"){
health -=1.0 * playerLevel;
checkhealth();
}
}
function checkhealth(){
var level = gameObject.Find("Player").GetComponent(Level);
var playerExp = level.experience;
if(health<=0.0){
playerExp += 50.0;
Instantiate(explosion, transform.position, transform.rotation);
Destroy (gameObject);
}
}
Answer by Bunny83 · Jun 05, 2011 at 08:16 PM
Your experience
variable is of type float (which is a value-type, not a reference type like a class). this line just copies the value into a temporary variable:
playerExp = level.experience;
If you change the temporary variable the variable in your level script won't change. Use either directly level.experience += 50.0
or assign the temp variable back to level.experience
.
Thank you...so much.. I've been trying to figure this out for about 15 $$anonymous$$utes. I'm still relatively new to JavaScript. I don't know what a float variable is XD
Float is short for floating point which is a decimal value (example: var three : float = 3.14;). An int (integer) is a number without decimal (example: var three : int = 3;). Basically they are the two user variables used together with calculations. Basic text is called a string which is set by quotations (example: var three = "3";).
To convert between them you can use:
numberVariable.ToString()
parseInt(stringOrFloatToIntVariable)
$$anonymous$$athf.RoundToInt(floatToIntVariable) <- (this one has lots of options)
thank you xD would you be mad If i put that on my blog? If you want I can say I got it from you and give people a link to your unity answer acount