• 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
Question by Hamsterdogg55 · Jun 15, 2020 at 08:24 PM · instantiateprefabsinstantiate prefab

Destroying instantiated prefabs destroy all of my instantiated prefabs

I am making a top down shooter game and I want my enemy spawners to be able to be destroyed by the player. My issue is that when I destroy one spawner, the others are destroyed as well.

Here is my spawner Script:

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class spawnSpots : MonoBehaviour
     {
         // Start is called before the first frame update
         [SerializeField] GameObject[] enemy;
         
         private float timeBetweenSpawns;
         float startTimeBetweenSpawns;
         [SerializeField] float minTimeBetweenSpawns = 1;
         [SerializeField] float maxTimeBetweenSpawns = 3;
         public bool isPlayerNear = false;
         [SerializeField] float health = 10;
     
         [SerializeField] GameObject deathParticles;
         [SerializeField] GameObject[] paintSplatter;
         
     
     
         Collider2D Mycollider;
     
         // Start is called before the first frame update
         void Start()
         {
             startTimeBetweenSpawns = Random.Range(minTimeBetweenSpawns, maxTimeBetweenSpawns);
             timeBetweenSpawns = startTimeBetweenSpawns;
             Mycollider = GetComponent<Collider2D>();
     
         }
     
         // Update is called once per frame
         void Update()
         {
             if (isPlayerNear == true) 
             {
                 print("Spawning");
     
                 if (timeBetweenSpawns <= 0)
                 {
                     
                     
                     int randomEnemy = Random.Range(0, enemy.Length);
                     Instantiate(enemy[randomEnemy], transform.position, Quaternion.identity);
                     timeBetweenSpawns = startTimeBetweenSpawns;
                     print(randomEnemy);
                 }
                 else
                 {
                     timeBetweenSpawns -= Time.deltaTime;
                     
                 }
             }
             else
             {
                 print("Disabled");
                 return;
             }
         }
     
     
         private void OnTriggerEnter2D(Collider2D collision)
         {
             if (!CompareTag("PlayerSword"))
             {
                 isPlayerNear = true;
                 print("true");
             }
             else
             {
                 Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
             }
     
         }
     
         private void OnTriggerExit2D(Collider2D collision)
         {
             if (!CompareTag("PlayerSword"))
             {
                 isPlayerNear = false;
                 print("false");
             }
             else
             {
                 Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
             }
         }
     
         private void OnTriggerStay2D(Collider2D collision)
         {
             if (!CompareTag("PlayerSword"))
             {
                 isPlayerNear = true;
                 print("true");
             }
             else
             {
                 Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);
             }
         }
     
     
         public void TakeDamage()
         {
             health--;
             print(health);
     
             if (health <= 0)
             {
                 print("Called");
                 int randomSpatter = Random.Range(0, paintSplatter.Length);
                 Destroy(this.gameObject);
                 var deathPart = Instantiate(deathParticles, transform.position, Quaternion.identity);
                 var paint = Instantiate(paintSplatter[randomSpatter], transform.position, Quaternion.identity);
                 Destroy(deathPart, 2f);
                 Destroy(paint, 5f);
     
             }
         }
     
     }
 
 
 And here is my Collision Script:
 
 
 
 public class SpawnerCollider : MonoBehaviour
 {
     bool hit = false;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     private void OnCollisionEnter2D(Collision2D collision)
     {
         hit = true;
         if(hit && hit == gameObject.GetComponent<Collider2D>())
         {
             FindObjectOfType<spawnSpots>().TakeDamage();
             print("Collided");
         }
         hit = false;
         
 
     }
 }


Comment

People who like this

0 Show 0
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

Answer by ShadyProductions · Jun 16, 2020 at 05:27 AM

 private void OnCollisionEnter2D(Collision2D collision)
 {
     var spawn = collision.gameObject.GetComponent<spawnSpots>();
     if(spawn != null)
     {
         spawn.TakeDamage();
         print("Collided");
     }
 }

Try this

Note that FindObjectOfType will just return the first object of that type in the scene that it finds, So this:

 Physics2D.IgnoreCollision(FindObjectOfType<Projecile>().GetComponent<Collider2D>(), Mycollider);

Really doesn't look correct at all.

Also you probably want to compare tag with the object u actually collided with instead of the object the script is on instead?

 if (!collision.gameObject.CompareTag("PlayerSword"))
Comment

People who like this

0 Show 0 · 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

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

161 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer

Referencing prefab in code without using editor or Resources.Load? 0 Answers

Instantiate is spawning too many clones 2 Answers

How to randomly instantiate prefabs after a certain time? 2 Answers

Instantiated object that has a collision is not firing OnCollisionEnter2D 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