Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JoschiProd · Jun 26, 2013 at 09:55 PM · gameobjectinstantiate3dtext

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JoschiProd · Jul 14, 2013 at 08:43 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges