Hi there,
I’ve created a program using C# where on pressing the space bar, a bouncy cube is instantiated. The rotation is done so that when it bounces off the floor it heads towards the direction of a GameObject I created called “Wall”.
What I want to happen is that when the cube hits the wall, on collision, the wall is destroyed.
Unfortunately, the wall doesn’t get destroyed. The wall doesn’t have a rigidbody (but the cube does). Although, I have given the wall a rigidbody but I am still unable to destroy the wall.
Here is my code. Would be most grateful if someone could help me. I’m sure the answer is pretty obvious to the more experienced users out there.
public class creator : MonoBehaviour {
//place objects into scene after scenes begin
public Transform thePrefab;
void OnCollisionEnter (Collision collision) // Detect collisions with bouncy cube
{
if (collision.gameObject.name=="Wall") // if cube hits a gameObject called "Wall"
{
Destroy(collision.gameObject); // Wall would be destroyed
}
}
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonUp ("Space") )
{
Instantiate(thePrefab, transform.position, transform.rotation);
}
}
}