2

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

Ash
  • 59
  • 7
  • Code looks very tidy! Just to be clear, what is the problem at the moment ???? :O – Fattie Jan 23 '21 at 03:41
  • @Fattie When I add a second "Breakable" Prefab to the game I break one it breaks both of them because they have the same name and tag. I want to be able to use the same name and tags for multiple of the same prefab but have only the one I'm looking directly at be broken. How would I do that? if that makes sense – Ash Jan 23 '21 at 03:45
  • Ahhh! its simple ! – Fattie Jan 23 '21 at 03:52

1 Answers1

2

Fortunately it's easy and basic in Unity!

You don't do this:

breakableObject = GameObject.Find("Breakable");

The good news is the cast will tell you what object you hit! Great, eh?

It's basically:

hit.collider.gameObject

You can use hit.collider.gameObject.name in a Debug.Log for convenience.

It's that easy!

Note you then (if you need it) get your component on that object with something like .GetComponent<YourBreakishBlock>()... easy.

Note that you can CHECK if the object hit, is one of the "YourBreakishBlock" objects, by simply checking if that component is present.

Note simply google something like "unity physics raycast out hit, which object was hit ?" for endless examples,

https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/changing-properties-of-object-hit-with-raycast.538819/

etc.

--

Make this simple class:

public class ExamplePutThisOnACube: MonoBehavior
{
public void SAYHELLO() { Debug.Log("hello!"); }
}

Now put that on SOME cubes but NOT on OTHER cubes.

In your ray code:

ExamplePutThisOnACube teste =
  hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();

and then check this out:

if (teste != null) { teste.SAYHELLO(); }

Now run the app and try pointing at the various cubes!

halfer
  • 19,824
  • 17
  • 99
  • 186
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Wow thats incredibly easy xD I don't really understand what the .getcomponent part does because I haven't learned that yet! Now I need to figure out how to put the block back in the same place rather than where the prefabs position is! – Ash Jan 23 '21 at 04:04
  • Where do I put each one of them sorry? Brain is working at half speed right now! Which scripts would they go in? – Ash Jan 23 '21 at 04:17
  • wait i got it working! So could I use some form of getcomponent to get the position of the cube clicked, pass it into the respawn script and then set position like that?! omg OR get the entire object and pass that exact object (which would come with its position) to the respawner!? – Ash Jan 23 '21 at 04:26
  • I got it working! They work individually now!!!! Thank you SO SO SO MUCH! – Ash Jan 23 '21 at 04:43
  • yes both of your statements there are CORRECT !!!! BTW getting the position is this easy !! `hit.collider.gameObject.transform.position` !!! – Fattie Jan 23 '21 at 15:50
  • Awesome! I did something similar but i think slightly longer: `breakableObject = hit.collider.gameObject; objectPosition = breakableObject.transform.position;` – Ash Jan 23 '21 at 16:39
  • Now I need to make sure that if I break multiple, they don't all go back to the same position xD The bugs never end! – Ash Jan 23 '21 at 17:50