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 /
  • Help Room /
avatar image
1
Question by Galbes · Jul 02, 2020 at 07:22 PM · gameobjectspawnbullethit detection

Bullet hit a object, another object get destroyed

So this this my problem, i have a square that when destroyed spawn 4 smaller squares when shooting on it, but when i shoot one of the 4 smaller squares sometimes a diferent square that are not even close gets destroyed.

The problem is worse when it have multiple smaller squares because just shooting one of the squares continuously make the other ones get destroy except the one i'm shooting

Asteroid Code (Square)

 public class AsteroidHealth : MonoBehaviour
 {
     [SerializeField] private int maxHealth = 1;
     public int currentHealth;
     public GameObject miniAsteroid;
 
     // Start is called before the first frame update
     void Awake()
     {
         currentHealth = maxHealth;
     }
 
     // Update is called once per frame
     void Update()
     {
         DestructionAsteroid();
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if(collision.gameObject.tag == "Magnet")
         {
             currentHealth--;
         }
     }
     
     //Asteroid
     void DestructionAsteroid()
     {
         if (gameObject.tag == "Asteroid" && currentHealth < 1)
         {
             for (int i = 0; i < 4; i++)
             {
                 float randomPos = Random.Range(-10, 10);
                 Instantiate(miniAsteroid, transform.position + new Vector3(randomPos, 0, randomPos), transform.rotation);
             }
             Destroy(gameObject);
         }
     }  
 }
 

Mini Asteroid (Smaller Square)

 public class MiniAsteroidHealth : MonoBehaviour
 {
     [SerializeField] private int maxHealth = 1;
     public int currentHealth;
     private SpawnManager spawnManagerScript;
     private bool collisionMagnet = false;
 
     public GameObject ore;
 
     private GameManager gameManagerScript;
 
     // Start is called before the first frame update
     void Awake()
     {
         currentHealth = maxHealth;
         gameManagerScript = GameObject.Find("GameManager").GetComponent<GameManager>();
     }
 
     // Update is called once per frame
     void Update()
     {
         DestroyMiniAsteroid();
     }
 
     void DestroyMiniAsteroid()
     {
         if (gameObject.tag == "MiniAsteroid" && currentHealth < 1 && collisionMagnet == false)
         {
             spawnManagerScript = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
 
             spawnManagerScript.miniAsteroidDestroy++;
             float randomPos = Random.Range(-5, 5);
             Instantiate(ore, transform.position + new Vector3(randomPos, 0, randomPos), transform.rotation);
             Destroy(gameObject);
         }
     }
 
     void Absorption()
     {
         if (gameObject.tag == "MiniAsteroid" && currentHealth < 1)
         {
             spawnManagerScript = GameObject.Find("Spawn Manager").GetComponent<SpawnManager>();
             gameManagerScript.UpdateScore(5);
             spawnManagerScript.miniAsteroidDestroy++;
             Destroy(gameObject);
         }
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.tag == "Magnet")
         {
             currentHealth--;
             collisionMagnet = true;
             Absorption();
         }
     }
 }
 

Bullet Code

 public class BulletSettings : MonoBehaviour
 {
     [SerializeField] private float bulletSpeed = 100;
     private AsteroidHealth asteroidHealthScript;
     private MiniAsteroidHealth miniAsteroidHealthScript;
     public float lifeSpan = 1.5f;
     public int bulletDamage = 1;
 
     public GameObject explosion;
 
     // Start is called before the first frame update
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         Movement();
         DestroyAfterTime();
     }
 
     private void OnCollisionEnter(Collision collision)
     {
         Instantiate(explosion, transform.position, transform.rotation);
         if (collision.gameObject.tag == "Asteroid")
         {
             asteroidHealthScript = GameObject.FindWithTag("Asteroid").GetComponent<AsteroidHealth>();
             asteroidHealthScript.currentHealth -= bulletDamage;
         }
 
         if(collision.gameObject.tag == "MiniAsteroid")
         {
             miniAsteroidHealthScript= GameObject.FindWithTag("MiniAsteroid").GetComponent<MiniAsteroidHealth>();
             miniAsteroidHealthScript.currentHealth -= bulletDamage;
         }
         Destroy(gameObject);
     }
 
     void Movement()
     {
         transform.Translate(Vector3.forward * bulletSpeed * Time.deltaTime);
     }
 
     void DestroyAfterTime()
     {
         Destroy(gameObject, lifeSpan);
     }
 
 }
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 Imdeeo · Jul 03, 2020 at 06:14 AM

Hi there! The problem is in the "GameObject.FindWithTag" in the Bullet script. This method search for one gameObject in the scene with the given tag and returns the first found. You can directly use the collision.gameObject to get the component.

 collision.gameObject.GetComponent<MiniAsteroidHealth>();

Also, it's better to use OnTriggerEnter than OnCollisionEnter method for this kind of stuff because the first one is more efficient.

     private void OnTriggerEnter(Collider other)
     {
         Instantiate(explosion, transform.position, transform.rotation);
         if (other.CompareTag("Asteroid"))
         {
             asteroidHealthScript = other.GetComponent<AsteroidHealth>();
             asteroidHealthScript.currentHealth -= bulletDamage;
         }
 
         if (collision.gameObject.tag == "MiniAsteroid")
         {
             miniAsteroidHealthScript = other.GetComponent<MiniAsteroidHealth>();
             miniAsteroidHealthScript.currentHealth -= bulletDamage;
         }
         Destroy(gameObject);
     }

I hope this helps you!

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

279 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 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

Destroy Instantiate is not working 0 Answers

How do i spawn an object with a button press? 3 Answers

Spawning only 1 gameObject 3 Answers

Bullet works fine, but it's invisible 1 Answer

How to spawn randomly with multiple gameobject 2 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