- Home /
Destroy Gameobject on collision
So I have a controller script where I only want to destroy the game object when it matches tags and I happen to be using an ability. This ability is slam. However even though I have it programmed to need both conditions in order to destroy, the platform gets destroyed whether im using slam or not. no matter what. Please help.
using System; using UnityEngine; using System.Collections; using UnityEngine.UI;
namespace UnityStandardAssets.Vehicles.Ball { public class Ball : MonoBehaviour { // variables public float basespeed; public float jumppower; public float sprintspeed; public float currentspeed; public float slampower;
private Rigidbody rb;
private const float k_GroundRayLength = .8f; // The length of the ray to check if the ball is grounded.
public bool slam;
public bool grounded;
//function start
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
public void Move(Vector3 moveDirection, bool jump){
{
// basic movement
rb.AddForce (moveDirection * currentspeed);
}
}
void Update (){
{
//jumping sequence
if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength) && (Input.GetKeyDown (KeyCode.Space)))
rb.AddForce (new Vector3 (0, jumppower, 0));
}
{ //sprint sequence
if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength) && (Input.GetKey (KeyCode.LeftShift)))
currentspeed = sprintspeed;
else
currentspeed = basespeed;
}
{
if (Physics.Raycast (transform.position, -Vector3.up, k_GroundRayLength))
grounded = true;
else
grounded = false;
}
//Slam Sequence
{
if (Input.GetKeyDown (KeyCode.Q) && (grounded == false))
slam = true;
{
if (slam == true)
rb.AddForce (new Vector3 (0, slampower, 0));
}
}
}
void OnTriggerEnter(Collider collider)
{
{
if ((slam == true) && (collider.gameObject.tag == "Destroy"))
Destroy (GameObject.FindGameObjectWithTag ("Destroy"));
}
{
if (slam == true)
slam = false;
}
}
}
}
Answer by Grimmick · Jun 03, 2016 at 10:01 PM
Are you sure, slam is false when you think you did not use slam? As the slam field is public another script could have modified it or maybe you set the value to true in the editor. To be 100% sure you could temporarily replace slam by a private variable or use Debug.Log at the beginning of OnTriggerEnter to print the value of slam.
Are you sure, the platform is destroyed by the call to Destroy in OnTriggerEnter? So it's not destroyed anymore when you comment out the code in OnTriggerEnter? Maybe another script causes the platform to be destroyed.
Why do you destroy "one active GameObject tagged" instead of the game object that entered the trigger? Why don't you directly use collider.gameObject? Using GameObject.FindGameObjectWithTag (which does not appear in the documentation, but checking UnityEngine.dll shows that FindWithTag just calls GameObject.FindGameObjectWithTag) may lead to a different platform being destroyed.
Answer by $$anonymous$$ · Jun 04, 2016 at 10:20 AM
void OnTriggerEnter(Collider iWantToDestoryYou)
{
{
if ((slam == true) && (collider.gameObject.tag == "Destroy"))
Destroy (iWantToDestoryYou); //iWantToDestoryYou is gameobject which collided so it will destroy him
}
{
if (slam == true)
slam = false;
}
}
Your answer
Follow this Question
Related Questions
Colliding Objects 1 Answer
Is it possible to destroy an object only within the collider? 1 Answer
2d edge collider wont destroy? 0 Answers