• 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 /
avatar image
Question by dBlue · Jul 25, 2015 at 11:03 PM · c#collisiongameobjectdestroy

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;
 }

}

Comment

People who like this

0 Show 4
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 Alec-Slayden · Jul 25, 2015 at 11:19 PM 0
Share

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.

avatar image Hexer · Jul 25, 2015 at 11:24 PM 0
Share

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

avatar image Alec-Slayden · Jul 25, 2015 at 11:53 PM 0
Share

Perhaps, but something must call FloorCheck, or nothing is going to happen. I assume the other object is calling it?

avatar image dBlue · Jul 26, 2015 at 01:32 AM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image

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.

Comment
dBlue

People who like this

1 Show 0 · 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

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;
  }
 }
Comment
dBlue

People who like this

1 Show 2 · 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 dBlue · Jul 27, 2015 at 01:03 AM 0
Share

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.

avatar image Hexer · Jul 27, 2015 at 01:14 AM 0
Share

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.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

How to only delete one of two collided objects? 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Simple on Collision Help (C#) 2 Answers

Trouble with destroying an instantiated prefab 2 Answers

Collision Counter +1 To Counter Only Once When Collided 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