Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by meechan007 · Oct 15, 2015 at 07:09 PM · audioparticlesif-statementsdestroy objectvoid

How to destroy something after X hits

I am trying to destroy something after it has been hit twice. I followed this page: http://answers.unity3d.com/questions/128945/destroy-object-after-5-collisions.html But i can not make the object delete or make a sound after it has been hit twice.

using UnityEngine; using System.Collections;

public class Harder_bricks : MonoBehaviour {

 public int hit = 0;
 public GameObject brickParticle;
 public AudioClip Brick_breaking;


 void OnCollisionEnter(Collision other)
 {
     hit += 1;
 }

 void checkhit()
 {
     if (hit == 2)
     {
         AudioSource.PlayClipAtPoint(Brick_breaking, transform.position);
         Instantiate(brickParticle, transform.position, Quaternion.identity);
         GM.instance.DestroyBrick();
         Destroy(gameObject);

     }
 }

}

Any and all help is appreciated.

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

2 Replies

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

Answer by dkjunior · Oct 15, 2015 at 10:42 PM

You need to call checkhit() after incrementing:

 void OnCollisionEnter(Collision other)
  {
      hit += 1;
      checkhit();
  }
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
avatar image
0

Answer by SpaceFace15 · Oct 16, 2015 at 12:02 AM

Well bro, I'm kind of new at unity but I sorta understand. You should use a Raycast or a collider. For example if you're using a bullet to hit something:

 public float RayLength;
     public float Angle; // Ignore this
     private bool MyBool;
     public float SpeedRacer;
     public float DragOn; // Ignore this
 public GameObject Bullet;
 
 
 
 //Below this will create your raycast hit meaning if this hits something something else will happen.
 
 RaycastHit hit;
             //Ray creation
             Debug.DrawRay(transform.position, Vector3.down);
                Ray MyRay = new Ray(position this ray is initialized at, Vector3.whatever direction you want it pointing to);
             if (!MyBool)
             { //Creating the ray statement.
 //Making the ray attached to the object.
     if (Physics.Raycast(MyRay, out hit,  RayLength)
                    {
                     if (your raycast variable.the collider of the object you wanna hit.its tag == "Your object you wanna destroy's tag")
                     {
                         //Put what ever you want to happen here for your case you want to destroy 
                             Destroy(Your game object you want hit) 
     
                     }
                 }
             }
         }
                        
     
         }
    

 

If you want it on collision:

 void OnCollisionEnter(Collision YourGameobjectasAVarable)
 {
   if(YourGameobjectasAVarable.collider.tag == "YourGameObject'sTag")
         {
             //Whatever you want to happen, in your case you want it to be destroyed
             Destroy(//Your game object//*)
         }



Hope I helped bro, if you want to learn more check out unity's tutorials on scripting it's quite easy once you get it. Hope I said the right things lol.

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 meechan007 · Oct 16, 2015 at 10:53 AM 0
Share

I understand what you are saying but i was wanting something to be destroyed after two hits.

Thanks for the answer though!

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

33 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

Related Questions

Destroy Object on Collision (with di sound) 0 Answers

Help destroying a seperate game object on collision enter 1 Answer

Creating a background to have a random shape destroy effect 0 Answers

Play audio once while condition is met in update function (C#) 2 Answers

How to get particle velocity to affect audio amplitude 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