Changing a Variable from another script. JavaScript
So I'm trying to get a player to level up with XP. I have the xp needed and scaling in one script and detecting if a monster is killed as well. On the Monster script I have the damage the player does as well as sending if the monster is killed. I'm still new to this and looking online has not helped. I think the static variable only makes a copy in the second script and I am unable to change the value from the original script. Please tell me why it is not working and what is going on instead and how to fix it.
Script one with the set variables to be sent and later changed. public static var Enemy1 :boolean=true; public static var DamageX : float=1; public static var exp=1; public static var level=1f; public static var levelexp=0; public static var expneeded=2f; public static var DamageXL: float=DamageX; public static var expL=exp; public static var levelL=level; public static var levelexpL=levelexp; public static var expneededL=expneeded; public static var j= Mathf.Round(expneededL); public static var EM1=Enemy1;
function Update () {
exp=expL;
level=levelL;
j= Mathf.Round(expneededL);
DamageX= DamageXL;
if (EM1==true){
Debug.Log(Enemy1);
expL+=10;
Enemy1=false;
}
else {Enemy1=false;}
if (expL>=expneededL){
expL=expL-expneededL;
levelL=levelL+1;
expneededL=6/5*levelL*levelL*levelL-15*levelL*levelL+100*levelL-140;
DamageXL=1+(levelL/4);
}
}
I have a copy of each static made so I could see live updates in the game since the static variable could not be manipulated mid scene to me.
Here is the second script (parts that do not work)
public var damagelocal=Global.DamageX;
function OnTriggerEnter2D(obj) {
// Name of the object that collided with the enemy
var name = obj.gameObject.name;
var r2d = GetComponent("Rigidbody2D");
if (name == "Enemy") {
r2d.transform.position.y=r2d.transform.position.y;
r2d.transform.position.x=r2d.transform.position.x;}
if (name == "Player"){
r2d.transform.position.y=r2d.transform.position.y;
r2d.transform.position.x=r2d.transform.position.x;}
// If the enemy collided with a bullet
if (name == "ball 1(Clone)") {
health=health-1;
Destroy(obj.gameObject);
if (health<=0){
// Destroy itself (the enemy) and the bullet
Global.Enemy1=true;
Destroy(gameObject);
Destroy(obj.gameObject);}
}
// If the enemy collided with the spaceship
else if (name == "Slash Top(Clone)" ){
health=health-damagelocal;
Destroy(obj.gameObject);
if (health<=0){
// Destroy itself (the enemy) and the bullet
Destroy(gameObject);
Destroy(obj.gameObject);
Debug.Log("enemy killed by slash");
}
}}
The enemy tracks the player and stops if it hits a player or another enemy. If the enemy is destroyed it is suppose to update the boolean but it seems to update it only inside of this script. And changing the value manually seems to have no effect either so I am at a loss.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102641im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with variables 1 Answer
How can I make A Object have the same Scale as B Object? 0 Answers
compare variables JS 1 Answer
Why is my script only running when object has been selected beforehand? 1 Answer
Parameter for animation not updating 0 Answers