- Home /
Instantiated enemy doesn't work.
Hello
I created an "Enemy" that has its current health displayed above its head via a 3D Text that is constantly updated. It loses health when the "Bullet" Object, which I tagged as, Bullet, collides with the "Enemy". And it is constantly looking at me and moving towards me. When the distance between me and the "Enemy" is below 2 it decreses my own health.
I have the "Enemy" Object in my scene and made a function to spawn (Instantiate) another one by pressing a button.
I made sure that the script finds every GameObject var that it needs.
That's it for the theoretical part... Now when I run my game everything doesn't work how it should.
Everything works fine as long as I don't spawn another "Enemy".
When I spawn one, the Health won't get displayed on the 3D Text of the "Enemy" I spawned, instead, when I shoot it it decreases the Health displayed on the original "Enemy", and once it reaches 0 it destoyes the one I shot, and the original one has full health again.
After I destroyed these two "Enemys" and tried to spawn another one its health goes beyond 0 and I can't destroy it. Same for every other spawned "Enemy" after I killed the first one.
And instantiated "Enemys" aren't albe to cause my damage.
Here's my script that is attached to the "Enemy":
 //Vars
 var EnemyHealth : float = 100;
 var Enemy : GameObject;
 var EnemyNameTag : GameObject;
 
 
 //Kill Player Function Vars
 var EnemyObj : GameObject;
 var PlayerObj : GameObject;
 var GunScriptContainer : GameObject;
 var distance : float;
 
 //Shooting function
 var target : GameObject;
 
 
 function Awake()
     {
     Enemy = GameObject.Find("Enemy");
     EnemyNameTag = GameObject.Find("3DTextEnemy");
     GunScriptContainer = GameObject.Find("Glock");
     EnemyObj = GameObject.Find("Enemy");
     PlayerObj = GameObject.Find("First Person Controller");
     target = GameObject.Find("First Person Controller");
     };
  
 // Check if bullet hits enemy
 function OnTriggerEnter(BulletCol : Collider)
     {
     if (BulletCol.gameObject.CompareTag("Bullet"))
         {
         EnemyHealth -= Random.Range(3, 50);
         Debug.Log("Enemy down");
         };
     };
 
 //Update everything per frame
 function Update()
     {
     EnemyHealthNameController();
     ShootAtPlayer();
     PlayerDamageController();
     EnemyHealthController();
     };
     
 //Update Enemy Health
 function EnemyHealthNameController()
     {
     EnemyNameTag.GetComponent(TextMesh).text = "Enemy: "+EnemyHealth;
     };
 
 //Enemy Health function
 function EnemyHealthController()
     {
     if (EnemyHealth <= 0)
         {
         Destroy(gameObject);
         };
     };
 
 //Shooting/Looking at Player function
 function ShootAtPlayer()
     {
     transform.LookAt(target.transform);
     rigidbody.AddForce(transform.forward * 10);
     };
 
 //Player Distance Damage Function    
 function PlayerDamageController()
     {
     var distance = Vector3.Distance(EnemyObj.transform.position, PlayerObj.transform.position);
     if (distance <= 2)
         {
         GunScriptContainer.GetComponent(GunScript).PlayerHealth -= Random.Range(5, 10);
         };
     };
Any ideas? This problem has been bothering me for 5 days now. And is keeping me from making any progress in my first game.
Answer by Diekeke · Jun 27, 2013 at 12:42 AM
I also had a similar problem with my spawned enemies, what I did was to place my original enemy way beneath my map, so that I couldn't kill it or anything, I did that with all of my enemies that needed to be spawned, so I started to spawn those instead of the GameObjects in my Project tab, and it works just fine. Hope this helps you, let me know :)
Didn't try it yet, but yea... that might work but that can't be a permanent solution. Couldn't someone tell me why it isn't working?
Your answer
 
 
             Follow this Question
Related Questions
Reading XML file 0 Answers
Instantiated Objects not being set at ground/terrain level?(Solved) 1 Answer
Unwanted 2nd empty gameobject 1 Answer
Every NetworkViewID is 0? 0 Answers
NavMesh giving jerky like motion 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                