Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Jode13 · May 23, 2017 at 06:27 AM · 2d sprites

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.

Comment
Add comment · Show 3
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 SohailBukhari · May 23, 2017 at 07:04 AM 0
Share

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.

avatar image Jode13 · May 27, 2017 at 02:17 PM 0
Share

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

screengrabofaletter-compressed.jpg (71.5 kB)
avatar image Jode13 · Jun 20, 2017 at 08:19 PM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

alt text

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.


screengrabofaletter-compressed.jpg (71.5 kB)
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 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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a 2D Ragdoll 2 Answers

Large Image/Sprite Renderer components cause FPS drop on device 0 Answers

2D RPG boomerang instantiation help. 2 Answers

Can you add sprites to existing sprite sheet without breaking the tile pallet and in-game tiles 0 Answers

Moving a 2D character along the X and Z axis only. 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