• 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 timo0060 · May 28, 2012 at 06:28 PM · javascripterrorenemies

When I kill one enemy, the rest disapear, but still shoot

So I have made my enemy a prefab and when I enter a invisible trigger box, it starts to spawn them. The only problem is that when I shoot one the rest disappear, but I can still shoot them even though they're invisible now, and they'll still shoot back. I'm also getting the following error 3 times as soon as I run the game: NullReferenceException: Object reference not set to an instance of an object. Any help on how to solve this problem would be most welcomed.

Here's my code for spawning the enemies(Attached to the spawning area):

 var Enemy: Transform;   var SpawnEnemies: int = 0;
 
 var spawn = false;
 
 
 function OnTriggerEnter(other: Collider){
 
     if(other.tag == "Player" && spawn == false)
     {
         InvokeRepeating ("Spawn",1,1);
         spawn = true;
     }
 
 }
 
 function Spawn(){
 
     var instantiatedenemy = Instantiate(Enemy,GameObject.Find("EnemySpawn").transform.position, transform.rotation); 
     SpawnEnemies +=1;
     
     if(SpawnEnemies >=10)
     {
         CancelInvoke("Spawn");
         spawn = false;
     }}

My code for when the enemy is destroyed is (attached to enemy):

 if(life <= 0)
     {
         Destroy(gameObject);
     }

Code for when a bullet hits the enemy (attached to bullet):

    function OnTriggerEnter(enemy : Collider){
     if(enemy.tag == "Enemy")
     {
         Destroy(gameObject);
         EnemyScript.life -=1;
     }
 }
Comment
Add comment · Show 2
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 whydoidoit · May 28, 2012 at 06:32 PM 0
Share

Yep, going to need the code alright :)

Better post the code that makes an enemy disappear to start with - probably the spawning code too.

avatar image timo0060 · May 28, 2012 at 07:45 PM 0
Share

I'll edit the question to include it. Thank you for letting me know which code would be needed.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · May 28, 2012 at 07:59 PM

EnemyScript.life is a static isn't it? Got to be that - you need it to be a normal variable, otherwise they all share it.

  public var life : float;

Then in OnTriggerEnemy:

  if(enemy.tag == "Enemy") {
      Destroy(gameObject);
      enemy.GetComponent(EnemyScript).life -= 1;
 }
Comment
Add comment · Show 3 · 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 timo0060 · May 28, 2012 at 08:41 PM 0
Share

That worked perfectly. Thank you very much. This doesn't have to do with the original question, but I'm sure it's an easy fix, and I don't want to start a new question for the second time in 3 hours. When my enemies shoot, they all shoot from on spawn point of one enemy, when they should all be shooting from their own spawnpoints attached to them. This is my shooting code:

allowfire = false; var instantiatedbullet = Instantiate(bullet,GameObject.Find("EnemyGunSpawn").transform.position, Quaternion.Euler(direction.normalized)); yield WaitForSeconds(0.75); allowfire = true;

EnemyGunSpawn is where the bullets shoot from, and the direction is towards the player. I know that they all shoot from the first spawned enemies EnemyGunSpawn, but I'm not sure how to have it so they shoot from their own. Thank you for any help on this and all the help you've given.

avatar image whydoidoit · May 28, 2012 at 08:46 PM 0
Share

Actually, do start a new question - we can get the formatting right then and I'll be able to read it :)

avatar image timo0060 · May 28, 2012 at 08:49 PM 0
Share

I'll do that right now then. Thank you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy Other 2 Answers

A node in a childnode? 1 Answer

Scripting Error Help Needed 2 Answers

Error BCE0044 JavaScript 1 Answer

Incremental game need help 1 Answer


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