- Home /
Destroy 2d sprite object
Hi, I know this sounds simple but I have look on all the forums and used all suggestions and it should be working but I cannot get my sprites to be destroyed. Quick overview of my game and setup - This is a 2d Alphabet game, I imported the letters on a .PNG sheet and a created a prefabs from the sprite sheet. On the A prefab I have added; - Sprite renderer and added the imported sprite. - Edge collider and tick the IsTrigger. - Rigidbody 2D and I think left this as default. In the main menu created a GameObject and called it GameController, this is where I have a script to call the prefabs into the game and place randomly and this works. But when I try and add a destroy script to the Prefab A or on the GameController nothing gets destroyed. I am expecting the destroy script to be attached to the A prefab and on MouseDown destroy the game object but it does not. Here is my Destroy script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestoryUpperCase : MonoBehaviour
{
void OnMouseDown ()
{
Destroy(gameObject);
}
}
This seems really easy but I think I am using Unity interface wrong with the sprites, please can some advise on how I can get this to destroy. Thank you.
On$$anonymous$$ouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. So you don't attach collider on your sprite, if you want to destroy your sprite using On$$anonymous$$ouseDown then attach collider on sprite and call
void On$$anonymous$$ouseDown()
{
Destroy(gameObject);
}
On$$anonymous$$ouseDown is not called on objects that belong to Ignore Raycast layer. On$$anonymous$$ouseDown is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true. On$$anonymous$$ouseDown can be a co-routine. This event is sent to all scripts attached to the Collider or GUIElement.
$$anonymous$$y A sprite Prefab has a collider, is ticked for 'IsTrigger' and the Destroy script added as a component. ![alt text][1] Here is a screen shot of the game in play mode showing the 'A' sprite prefab components. But when clicking on the 'A' in the game it does not get destroyed.
@SohailBukhari [1]: /storage/temp/94859-screengrabofaletter-compressed.jpg
I have tired Physics.queriesHitTriggers is true." Try setting isTrigger to false or setting Physics.queriesHitTriggers to true but nothing happen. Also change to the BoxCollider2D and also did the Physics.queriesHitTriggers steps but with no success.
I Instantiate the GameObjects in one script and trying to destroy with a destroy script on the GameObject it's self. Should I be destroying the GameObject within the Instantiate script, such as void Update() for each frame?
Answer by Jode13 · May 29, 2017 at 06:35 AM
My A sprite Prefab has a collider, is ticked for 'IsTrigger' and the Destroy script added as a component.
Here is a screen shot of the game in play mode showing the 'A' sprite prefab components. But when clicking on the 'A' in the game it does not get destroyed.
Answer by mwnDK1402 · May 27, 2017 at 03:21 PM
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
"This function is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true." Try setting isTrigger to false or setting Physics.queriesHitTriggers to true.
It also sounds odd that you would use an EdgeCollider2D to check for mouse clicks. Try replacing it with a BoxCollider2D.