- Home /
gameObject doesn't self-destruct after collision c#
When the two objects collide, instead of one being destroyed, they both touch each other. I'm not sure what's wrong, is there something in my code?
using UnityEngine; using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void FloorCheck (Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}
are you calling OnCollisionEnter somewhere I'm not seeing? If not you may need to change Floorcheck to that. I don't see where Floorcheck is called.
I dont think there is an OnCollisionEnter in his script. He means when 2 objects hits eachother. One of them with this script attached. If that object isn't within the collider called floor. Then that object should disappear.
Be sure that both colliders doesnt have the isTrigger set to true. If it does, set them both to false. + add ridigbodies to them
Perhaps, but something must call FloorCheck, or nothing is going to happen. I assume the other object is calling it?
Hexer has it right, but I think Alec is right too, I never do call FloorCheck. Where or how should I do that, though? I'm having a hard time finding out where I can do that
Answer by Alec-Slayden · Jul 26, 2015 at 01:39 AM
It doesn't seem like you're actually making use of the collision event.
Consider replacing FloorCheck with OnCollisionEnter, (using the same method contents). OnCollisionEnter is automatically called when two colliders hit each other, and it uses Collision as a parameter too.
You could also call FloorCheck from the other object if it has an OnCollisionEnter method, passing the collision info, but I would advise letting your Upward class handle itself in its own method.
If you plan to use FloorCheck independently elsewhere, then I recommend adding OnCollisionEnter, and inside of that making a call to FloorCheck.
Answer by InfernoZYB · Jul 26, 2015 at 02:07 AM
Umm this is your script. It was missing the OnCollisionEnter... If this is right then make sure you check the above peoples answers as right because i just put theirs into your script. using UnityEngine; using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void OnCollisionEnter(Collision collision) {
FloorCheck(collision);
}
void FloorCheck (Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}
You could do that or...
using UnityEngine;
using System.Collections;
public class Upward : MonoBehaviour {
public float StartingPosition;
public Vector3 AntiGravity;
public bool FloorTouch = false;
void OnCollisionEnter(Collision col){
if (col.gameObject.tag == "Floor") {
FloorTouch = true;
} else {
FloorTouch = false;
}
}
void Start () {
float RandomFl = Random.Range(-0.5f, 0.5f);
float StartP = StartingPosition;
transform.position = new Vector3 (RandomFl, StartP, -1f);
gameObject.GetComponent<Rigidbody2D>().freezeRotation = true;
}
void Update () {
if (transform.position.y >= 4) {
float RandomFl = Random.Range (-0.5f, 1f);
transform.position = new Vector3 (RandomFl, -4f, -1f);
gameObject.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
transform.rotation = Quaternion.identity;
}
if (!FloorTouch) {
Destroy (gameObject);
}
transform.position += AntiGravity;
}
}
Yeah, that didn't work, on play the object with the script immediately disappears. Does anybody know what might cause that? Both objects spawn at different places, there's no way they are colliding.
If the object is a little bit above the floor, would it be just 0.01y. The object will disappear. This means that the object was destroyed before it could check if it was going to have a collision with the floor.