- Home /
increase a variable of a component script
Hi I've got a really basic Question: I've got a Script attached to an enemy and i want to increase a variable of an other script when the enemy is dead
var MaxLife : int = 100;
var CurrentLife : int;
var RagDoll : Transform;
var Enemy : GameObject;
var Spawnscript : spawnscript;
function Start () {
CurrentLife = MaxLife;
Spawnscript = GetComponent(spawnscript);
}
function ApplyDamage(Damage : float){
if(CurrentLife < 0){
return;
}
CurrentLife -= Damage;
if(CurrentLife < 1){
var ragdoll = Instantiate(RagDoll, Enemy.transform.position, Quaternion.identity);
Destroy(gameObject);
Spawnscript.Score += 1;
}
}
Is it score you want to increase? Is it on another game object?
If the Spawnscript is attached to the same gameobject as the script you have posted, you are destroying the instance of the Spawnscript too.
Answer by TheDavil86 · Jul 10, 2012 at 04:20 PM
You have to first find the object that contains the script. So you could either create a public var of type GameObject and drag the object using the score into it, then you could say for example:
var object : GameObject;
// Add 1 to the score
object.GetComponent("Score").score++;
The other solution is to use Find for the game object. So instead you can say:
GameObject.Find("ScoreObject").GetComponent("Score").score++;
Answer by fafase · Jul 10, 2012 at 04:35 PM
Now to access, if the script holding the score is on the same object (I doubt it) it won*t do anything but I guess it is on the player.
var other:SpawnScript;
function Start(){
other = GameObject.Find("Player").GetComponent(SpawnScript); //I reckon it is on the player
}
if(CurrentLife < 1){
var ragdoll = Instantiate(RagDoll, Enemy.transform.position, Quaternion.identity);
other.Score += 1;
Destroy(gameObject); //Note: no matter where you place that one, it is called once the update is done.
}
OR!!!!
in the SpawnScript:
static var Score:int;
then you access it anywhere else with:
SpawnScript.Score +=1;
This is fine since you have one socre only in the game.
Answer by f34rtehninja · Jul 10, 2012 at 04:42 PM
If you want to get a reference to the spawnscript on another gameobject you would need to do
var EnemyManagerOrSomething = GameObject.Find("ScriptGameObjectNameHere");
EnemyManagerOrSomething.GetComponent(spawnscript);
If the spawnscript is attached to the Enemy, and you have two scripts attached to the Enemy object than your GetComponent line will work.
Another issue you have is CurrentLife is an int and Damage is a float. Then you do CurrentLife -= Damage, which will throw an error or at least a warning. Current Life could be changed to a float and then just don't display any precision on it when you display it.
The instantiate line should work, putting a RagDoll at the transform.position with rotation of Quaternion.identity which I believe is what you want.
The Destroy(gameObject) line will destroy the object that the script is attached to.
Spawnscript.Score += 1; Will work if the Score is a public variable in the other script, the referencing and syntax works as long as you set up Spawnscript correctly.
Everything else should work. Could you share what particular problems or warnings your having? Is it a compiler error or a behavior error? As in does the console give you red text and you can't play or are the enemies doing weird things in game?
Note: The tendency is to have all members/fields, which are the variable names like maxLife and currentLife, start with a lower case letter then capital letter for all the following new words and then all function and script names start with a capital letter like ApplyDamage (which is correct) and SpawnScript and a capital for new words. You're welcome to do whatever you like, but the general trend is to follow those guidelines. When you try and refactor -> rename in MonoDevelop it doesn't even let you change the names unless they follow those rules, member names start with a lowercase, function names start with an Uppercase.
Answer by TheDarkSeal · Jul 10, 2012 at 06:55 PM
Thanks for the fast response. My Problem was my score variable in my other script wasnt static and GameObject.Find("ScoreObject"). This is my first Question in Unity Answers and I'm suprised how friendly you people are. Sorry for my bad english I'm German.