Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by motionlife89 · May 16, 2014 at 05:43 AM · collision

c# Destroy upon collision isn't working

I have runner who is supposed to destroy another game object "food" which is placed in my prefabs folder. Unfortunately, the code isn't working as expected. I would like to know what went wrong and how do I make the runner destroy "food" once the two collides?

I am attaching the screenshot of runner:

http://i59.tinypic.com/rqxcbc.jpg

This is the c# code of runner:

 using UnityEngine;
 using System.Collections;
 
 public class runner : MonoBehaviour {
 
 
     // The force which is added when the player jumps
     // This can be changed in the Inspector window
 public Vector2 jumpForce = new Vector2(0, 1);
 
 
 
     // Use this for initialization
     void Start () {
              
 
         
         // destroy food upon collision
         
         Destroy (GameObject.FindWithTag("food"),3);// 20sec
 
 
     }
 
     // Update is called once per frame
     void Update () {
 
 
     
         // Jump
         if (Input.GetKeyUp("space"))
         {
             rigidbody2D.velocity = Vector2.zero;
             rigidbody2D.AddForce(jumpForce);
         }
         // Die by being off screen
         Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
         if (screenPosition.y > Screen.height || screenPosition.y < 0)
         {
             Die();
         }
     }
 
     // Die by collision
     void OnCollisionEnter2D(Collision2D other)
     {
         Die();
     }
     
     void Die()
     {
         Application.LoadLevel(Application.loadedLevel);
     }
 }
 
 
 

Moreover, this is the screenshot of "food".

http://i57.tinypic.com/2dw7z10.png

This is the c# code of "food".

 using UnityEngine;
 using System.Collections;
 
 public class foodpow : MonoBehaviour {
 
     public Vector2 velocity = new Vector2(-4, 0);
     
     // Use this for initialization
     void Start()
     {
         rigidbody2D.velocity = velocity;
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Andres-Fernandez · May 16, 2014 at 06:31 AM

Your code does a completely different thing. Placing

 Destroy (GameObject.FindWithTag("food"),3);// 20sec

in your start function will destroy the first food object found 3 seconds after the start of the scene.

You should check for that in your OncollisionEnter2D function instead:

 // Die by collision
 void OnCollisionEnter2D(Collision2D other) {
         if (other.gameObject.CompareTag("food")) {
             Destroy (other.gameObject);
         } else {
             Die();
         }
     }
Comment
Add comment · Show 1 · 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 Andres-Fernandez · May 16, 2014 at 06:52 AM 0
Share

Also I'm not sure but this code might also kill the player when it touches the ground after jumping. In that case, check against the "floor" tag too (and if it's true then do nothing, just return).

avatar image
0

Answer by motionlife89 · May 16, 2014 at 01:56 PM

@Andres Fernandez :

Thanks for responding to my question! Just a follow up questions though, when I debug the code, an error message appears at the last "}" in the code you posted saying " expected".

May I ask why I'm getting this error? I just copied the code you posted. should I retain this code or remove it instead?

void Die() { Application.LoadLevel(Application.loadedLevel); } }

Comment
Add comment · Show 1 · 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 Andres-Fernandez · May 19, 2014 at 07:40 AM 0
Share

If you want to reload the scene when you die, the code seems correct

 void Die(){
    Application.LoadLevel(Application.loadedLevel);
 }

It looks like you have some syntax error. I suggest you check the code (and make sure each function has its curly brackets pair, and no semicolon is missed).

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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Collision detection for sprite 1 Answer

Moving Platforms and Collision 1 Answer

2D:I have a movementController script on my character but it doesn't move forward 0 Answers

Unity2D collision physics not exact 0 Answers

Need help on fast and efficient logic of destroying random balls. 0 Answers


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