How do I single out GameObject being detected by Raycast in a destroy / respawn system

I’m trying to make a Destroy gameobject, wait x seconds, respawn gameobject system. I have 2 scripts and I’m destorying then instantiating it again. I want to use multiples of the same prefab called “Breakable” but have only the one I’m aiming at being destroyed. Similar to games like Minecraft, aim and only the aimed at the block is destroyed.

BlockBreakItem script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlockBreakItem : MonoBehaviour
{
    RaycastHit hit;
    int layerMask = 1;
    public GameObject breakableObject;
    public bool isObjectDestoryed = false;

    public int score = 0;

    // Update is called once per frame
    void Update()
    {
        breakableDetection();
    }

    void breakableDetection()
    {
        Ray rayLocation = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(rayLocation, out hit, 1000, layerMask))
        {
            print("Detected");
            if (Input.GetKey(KeyCode.Mouse0))
            {
                
                breakableObject = GameObject.Find("Breakable");
                Destroy(breakableObject);
                isObjectDestoryed = true;
                
                score = score +1 ;
            }
        }
    }
}

RespawnBrokenObject script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RespawnBrokenObject : MonoBehaviour
{
    private BlockBreakItem BlockBreakItem;
    public GameObject breakablePrefab;

    // Start is called before the first frame update
    void Start()
    {
        BlockBreakItem = GameObject.FindObjectOfType<BlockBreakItem>();
    }

    // Update is called once per frame
    void Update()
    {
        if (BlockBreakItem.isObjectDestoryed == true)
        {
            Invoke("respawnObject", 5.0f);
            BlockBreakItem.isObjectDestoryed = false;
        }
    }

    void respawnObject()
    {
        GameObject tempObjName = Instantiate(breakablePrefab);
        
        tempObjName.name = breakablePrefab.name;
        BlockBreakItem.breakableObject = tempObjName;
    }
}

I hope the code isn’t too messy! Always worried it won’t be understood xD Thank you!

Your code is actually neater than most posters on this site. Anyways, I can see that you aren’t using the RaycastHitInfo from your raycast. This is essential in what you want to do, as it tells you what object you hit, and other info you might want, about the raycast.


Your method breakableDetection() should look like this:

    void breakableDetection()
    {
        if (!Input.GetKey(KeyCode.Mouse0))
            return;

        // This code is reached when:
        // left mouse click is detected
        Ray rayLocation = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (!Physics.Raycast(rayLocation, out RaycastHit hit, 1000, layerMask))
            return;

        print("Detected");

        // What object you hit with the raycast
        GameObject hitObject = hit.collider.gameObject;

        if (!hitObject.CompareTag("Breakable"))
            return;

        // This code is reached when:
        // raycast hit an object tagged as breakable
        Destroy(hitObject);
        isObjectDestoryed = true;
        score = score + 1;
    }

I try to prevent nesting as much as possible, so you’ll see a lot of return’s in my code. What I mean is:


I don’t do this:

if (true)
{
    if (trueagain)
    {
        print("Is true twice");
    }
}

I do this:

if (!true)
    return;

if (!trueagain)
    return;

print("Is true twice");

Just something you might want to try to write neater, more readable code. @just_insanity