I am trying to destroy something after it has been hit twice. I followed this page: Destroy object after 5 collisions - Questions & Answers - Unity Discussions
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.
You need to call checkhit() after incrementing:
void OnCollisionEnter(Collision other)
{
hit += 1;
checkhit();
}
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.