- Home /
How can I assign a clone with a script to a variable? without drag & drop
At the start of my game, a player(clone) is instantiated with the PlayerStats script on it. When an Enemy dies, it calls the adjust methods in the PlayerStats script to increase the score and money variables. So I dragged and dropped the Player prefab into the playerStats slot in the Enemy prefab but this doesn't adjust the values when I run the game. However, if I drag and drop the Player(Clone) to the playerStats slot on an Enemy(Clone) during run time then it works fine. But I obviously can't drag and drop this on every single enemy during run time.
So how can I set that playerStats slot to be player(clone) in the code? I've searched around a lot and I can't seem to find anything about this problem specifically.
 public class Enemy : Entity {
 
     //how much score or money the player gets after kill
     public int scoreReward;
     public int moneyReward;
     public PlayerStats playerStats;
     public GameObject player;
 
     void start(){
         this.scoreReward = 10;
         this.moneyReward = 10;
         player = GameObject.FindWithTag("Player");
         //playerStats = player.GetComponent<PlayerStats>();
     }
 
     ...
     
     public void Die(){
         Destroy (this.gameObject);
         player.GetComponent<PlayerStats> ().AdjustScore (scoreReward);
         //playerStats.AdjustScore (scoreReward);
         //playerStats.AdjustMoney (moneyReward);
     }
 }
Answer by getyour411 · Apr 07, 2014 at 01:57 AM
 change start() to Start()
 remove the // from playerStats in Start()
in Die(), move the Destroy to last
 change player.GetComponent<PlayerStats>()
to
 playerStats
O$$anonymous$$G thank you so much. I need to take a break lol. Those were dumb mistakes on my part.
Your answer
 
 
             