Estoy tratando de hacer un objeto de juego Destroy, esperar x segundos, reaparecer el sistema de objeto de juego. Tengo 2 scripts y estoy destruyendo y luego instanciando nuevamente. Quiero usar múltiplos del mismo prefabricado llamado "Breakable", pero solo tengo el que quiero destruir. Similar a juegos como Minecraft, apunta y solo se destruye lo que apunta al bloque.
Script BlockBreakItem:
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 ; } } } }Guión RespawnBrokenObject:
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; } }¡Espero que el código no sea demasiado complicado! Siempre preocupado de que no se entienda xD
¡Afortunadamente es fácil y básico en Unity!
No haces esto:
breakableObject = GameObject.Find("Breakable");¡La buena noticia es que el elenco te dirá qué objeto golpeas ! Genial, ¿eh?
es básicamente:
hit.collider.gameObject Puede usar hit.collider.gameObject.name en un Debug.Log para mayor comodidad.
¡Es fácil!
Tenga en cuenta que luego (si lo necesita) obtenga su componente en ese objeto con algo como .GetComponent<YourBreakishBlock>() ... fácil.
Tenga en cuenta que puede COMPROBAR si el objeto golpeado es uno de los objetos "YourBreakishBlock", simplemente comprobando si ese componente está presente.
Tenga en cuenta que simplemente busque en Google algo como "unity physics raycast out hit, what object was hit?" para un sinfín de ejemplos,
https://forum.unity.com/threads/getting-object-hit-with-raycast.573982/
https://forum.unity.com/threads/cambio-de-propiedades-de-objeto-golpeado-con-raycast.538819/
etc.
--
Haz esta clase simple:
public class ExamplePutThisOnACube: MonoBehavior { public void SAYHELLO() { Debug.Log("hello!"); } }Ahora pon eso en ALGUNOS cubos pero NO en OTROS cubos.
En tu código de rayos:
ExamplePutThisOnACube teste = hit.collider.gameObject.GetComponent<ExamplePutThisOnACube>();y luego mira esto:
if (teste != null) { teste.SAYHELLO(); }¡Ahora ejecute la aplicación e intente señalar los distintos cubos!