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 Scrap_Computer · Apr 24, 2013 at 07:21 PM · prefabclicksingle

Destroy a single prefab with a click ?

Is it possible to destroy a prefab with a click and not destroy the entire object making all of the prefabs destroy themselves.

I'm creating a game where i place prefabs (of a cube) on a plain and pan the camera down the pre placed prefabs, once clicked they should be destroyed and add score, but when i destroy the cube on click all of the prefabs are destroyed.

Am i using prefabs wrong or should i maybe change my way of making a mass amount of objects that can easily be generated/placed and destroyed individuality. Sorry i'm new to unity so alot of detail would help thanks so much for reading. Darren

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 OpenCoffee · Apr 24, 2013 at 07:47 PM 1
Share

The first thing that you should do is to explain a little bit further how you are doing the actual "clicking". Is it a simple raycast that you are using?

avatar image Scrap_Computer · Apr 25, 2013 at 03:33 PM 0
Share

I don't even know what a raycast is, can you explain it for me? I am clicking the gameobject cube that is from a prefab with my mouse and want to delete it. Simple but now for me :(

avatar image AlucardJay · Apr 25, 2013 at 03:40 PM 0
Share
  • Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

  • Raycasting Basics : http://www.youtube.com/watch?v=Sj4oxfQqzhA

  • Raycasting Basics : http://www.unity3dstudent.com/2010/08/intermediate-i01-raycasting/

2 Replies

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

Answer by Z2CSharp · Apr 25, 2013 at 01:58 AM

     private Ray ray; // The ray
     private RaycastHit hit; // What we hit
     
     void Update()
     {
         ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Ray will be sent out from where your mouse is located    
         if(Physics.Raycast(ray,out hit, 1000.0f) && Input.GetMouseButtonDown (0)) // On left click we send down a ray
         {
             Destroy (hit.collider.gameObject); // Destroy what we hit
         }
     }

This will work to only destroy the gameObject we hit when you left click.

Comment
Add comment · Show 3 · 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 Scrap_Computer · Apr 25, 2013 at 04:33 PM 0
Share

It doesn't seem to work i run into complication errors. $$anonymous$$ore than likely it is me not adaption the script to reference the objects in my code? Thanks for the help anyway buddy much appreciated!

avatar image AlucardJay · Apr 25, 2013 at 04:37 PM 0
Share

This script is in C#. $$anonymous$$ost likely you are after uJS. Check the links on my previous comment, it's all there.

avatar image Scrap_Computer · Apr 29, 2013 at 03:49 PM 0
Share

Thank you alucardj, works perfectly, still a noob at unity :) $$anonymous$$uch appreciated.

avatar image
0

Answer by Aminjon_Warshav · Dec 08, 2016 at 06:47 AM

@Scrap_Computer DontForget to use BoxCollider and GraphicRaycast

    using UnityEngine;
     using UnityEngine.EventSystem;
     public class DragToys :  MonoBehaviour, IPointerClickHandler
     {
        int HowManyTap;
         float timeDoubleTap;
         bool isDoubleClick;
     public void OnPointerClick (PointerEventData evenData)
         {
             isDoubleClick = true;
             HowManyTap++;
     
             if (HowManyTap == 2 && timeDoubleTap <= 0.2f && isDoubleClick) 
             {
                 Destroy (gameObject);
             }
             
         }
         
     
         void Update ()
         {
             if (isDoubleClick) 
             {
                 timeDoubleTap = timeDoubleTap + Time.deltaTime;
             }
              if (timeDoubleTap >= 0.2f) 
             {
                 timeDoubleTap = 0f;
                 isDoubleClick = false;
             }
     
         }
     }
     }
 
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

15 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

Related Questions

Multiple Cars not working 1 Answer

Muzzleflash help 0 Answers

A node in a childnode? 1 Answer

Component Value Reverts to Prefab Value under certain conditions when Scene is Tested 1 Answer

Unity fire1 prefab in code 2 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