Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Pathojen · Mar 26, 2019 at 07:24 PM · 2dscripting problem2d gamedestroy

Destroy script turns objects into ghosts

Hello. I have been researching all day, and I still can't find any answers for this. As implied, my destroy script has not destroyed the game objects. Instead, their collisiders get removed, and they can fly around like phantoms. I am trying to destroy two 2D objects, each with this script. Any advice on what I'm doing wrong?

Thanks in advance. Here is the script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DestroyCollision : MonoBehaviour
 
 { 
     // Start is called before the first frame update
     void OnCollisionEnter2D(Collision2D other)
     {
         //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
         if (other.gameObject.CompareTag("GameController"))
         {
            
            Destroy(gameObject);
         }
     }
 }
 
Comment
Add comment · Show 16
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 Bonfire-Boy · Mar 26, 2019 at 07:31 PM 0
Share

I've reformatted so your code is readable. For future reference, to post code, press the button with 101010 on it, then paste the code into the dialog the comes up.

avatar image mchts · Mar 27, 2019 at 08:53 AM 0
Share

I think this question is about where you attach your DestroyCollision. Is that attached directly the gameobject itself or a child component?

avatar image Pathojen · Mar 27, 2019 at 10:06 AM 0
Share

It is directly attached to the gameobject which is to be destroyed.

avatar image Bonfire-Boy · Mar 27, 2019 at 10:23 AM 0
Share

And that's your actual code you've shown us? It does sound like you're destroying a component or child, rather than the gameobject itself.

When you say that it has removed their colliders, are you basing that on their behaviour or have you actually looked at the objects in the editor to confirm this?

avatar image Pathojen · Mar 27, 2019 at 10:53 AM 0
Share

I'm basing it on behaviour, but now I have a new problem. I did change my code so I would use the same code for the object to be destroyed, and now my app crashes ins$$anonymous$$d of just causing the issue.

Here's the updated script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class $$anonymous$$ovement$$anonymous$$inus : $$anonymous$$onoBehaviour
 {
     public float movespeed;
 
     // Update is called once per frame
     void Update()
     {
         transform.Translate(movespeed * SimpleInput.GetAxis("Horizontal") / 10, movespeed * SimpleInput.GetAxis("Vertical") * -1 / 10, 0f);
     }
 
 
     // Start is called before the first frame update
     void OnCollisionEnter2D(Collision2D other)
     {
 
         //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
         if (other.gameObject.CompareTag("GameController"))
         {
             Destroy(gameObject);
         }
     }
 }
 
 
avatar image Bonfire-Boy Pathojen · Mar 27, 2019 at 12:17 PM 0
Share

What do you mean by "crashes"? Is the console showing an exception being thrown? Do you have any reason to think that this error has anything to do with the other one?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Pathojen · Mar 29, 2019 at 01:49 PM

After a long while, I found out how to do it. Instead of having the object destroy itself, I set up the collider to trigger Destroy.

If case anyone else has the same issue I did, here is the code I used.enter code here. I have a debug.log test which I used to test the trigger still attached.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Instantiator : MonoBehaviour
 {
     int magnet = 2;
 
 
 void OnTriggerEnter2D(Collider2D other)
 {
     //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
     if (other.gameObject.CompareTag("Player"))
     {
        magnet = magnet - 1;
             string converter = magnet.ToString();
             Debug.Log(converter);
             Destroy(other.gameObject);
     }
 }
 }

Comment
Add comment · 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
0

Answer by Bruno2907 · Mar 29, 2019 at 01:52 AM

Hi, you script seems to be correct, but what happens is that it will only destroy the GameObject this script is attached to (and consequently all of it’s children) but not any parent G.O.
I guess your collider and render are two distinct Game Object with the collider being children from the render, in this case you’d need to make a reference to the root GameObject in your script and destroy that one instead.

Comment
Add comment · 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

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

268 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 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 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 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 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 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 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 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 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 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 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 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

2D Top Down SmokeBomb 0 Answers

I am trying to spawn and move a asteroid but the spawnner script doesnot access the script attached to the asteroid. 1 Answer

Trying to get gameobject to point to mouse in a orbit around the player 2D 1 Answer

Mouse Click Walk to Idle Animations 0 Answers

Get a single number for rotation (2d game) when using Quaternion.FromToRotation 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