- Home /
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
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?
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 :(
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/
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.
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!
This script is in C#. $$anonymous$$ost likely you are after uJS. Check the links on my previous comment, it's all there.
Thank you alucardj, works perfectly, still a noob at unity :) $$anonymous$$uch appreciated.
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;
}
}
}
}
Your answer
Follow this Question
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