- Home /
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);
}
}
}
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.
I think this question is about where you attach your DestroyCollision
. Is that attached directly the gameobject itself or a child component?
It is directly attached to the gameobject which is to be destroyed.
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?
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);
}
}
}
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?
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);
}
}
}
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.
Your answer
Follow this Question
Related Questions
2D Linked Portals 0 Answers
Trying to change character model on mouse down 1 Answer
2D Top Down SmokeBomb 0 Answers
Trying to get gameobject to point to mouse in a orbit around the player 2D 1 Answer