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
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.
@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;
}
}
}
}