c# - Unity: PlayerLife variable doesn't update visibly in Inspector or HUD, but still functional in Background -




the playerlife variable doesn't update visibly in inspector or on-screen health text, player still dies because playerlife drops below zero.

i've determined player prefab attached zombie gameobject solely player prefab rather in-scene active player. how make zombies reference in-scene active player rather basic player prefab, script? (also, won't allow me manually drag active player zombie)

call hierarchy playerlife

public class player : monobehaviour {     public raycasthit hit;     public int gundamage = 1;     public zombie zombie;      private float hitforce = 100f;     public float playerlife;     private vector3 flarelower = new vector3(0, -0.5f, 0);      void start()     {         spawnpoints = playerspawnpoint.getcomponentsinchildren<transform>();         playerlife = 200;     }      void update() //t-toggle     {         if (input.getbutton("fire1"))         {             lazerbeam();         }          if (respawn != lasttoggle)         {             respawn();             respawn = false;         }         else             lasttoggle = respawn;     }        public void life (float damage)     {         playerlife -= damage;         if (playerlife <=0)         {             playerlife = 100;             scenemanager.loadscene(2);         }     }  } 

public class zombie : monobehaviour {     public int currenthealth;     public player player;     public playerlifecollider playercollider;      private int damage;      public void damage(int damageamount)     {         currenthealth -= damageamount;         if (currenthealth <= 0)         {             playerlifecollider.instance.objectsinrange.remove(gameobject);             destroyzombie();         }     }      public void destroyzombie()     {         destroy(gameobject);         // gameobject.setactive(false);     }      public void damageplayer(float damage)     {          player.life(damage);     } } 

as said, problem not referencing player object on scene, prefab one. avoid that, can add start function zombie script , ask should player instance in scene. this, can use findobjectoftype function:

void start() {     player = findobjectoftype<player>(); } 

considering have 1 player script in entire scene, can save in player class static reference player instance.

public class player : monobehaviour {     private static player _instance;      public static player instance     {                 {             if (_instance == null)             {                 _instance = findobjectoftype<player>();             }            return _instance;         }     }      // reset of class  } 

you can reference in zombie script:

public class zombie : monobehaviour {     static player player;      void start()     {         if(player == null)         {             player = player.instance;         }     }      // rest of class content } 

this way, have 1 call findobjectoftype function instead of once per object using zombie script.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -