Question by
Calenciaga · Feb 06, 2021 at 01:56 PM ·
instantiateraycastprefabsdestroy object
Deleting a gameobject/prefab that has been instantiated
Hello, I am basically a beginner but i'm to create different game features as practice. I followed this tutorial to instantiate prefab blocks using a raycast and a template block that shows where the block will be placed before it gets placed, then adds a clone of the prefab to the world. is anyone able to set me in the right direction of how to add on to this script so that another button will destroy the prefab that is hit by the raycast? thanks!
if (buildModeOn)
{
RaycastHit buildPosHit;
if (Physics.Raycast(playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out buildPosHit, 4, buildableSurfacesLayer))
{
Vector3 point = buildPosHit.point;
buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
canBuild = true;
}
else
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
}
if (!buildModeOn && currentTemplateBlock != null)
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
if (canBuild && currentTemplateBlock == null)
{
currentTemplateBlock = Instantiate(blockTemplatePrefab, buildPos, Quaternion.identity);
currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial;
}
if (canBuild && currentTemplateBlock != null)
{
currentTemplateBlock.transform.position = buildPos;
if (Input.GetMouseButtonDown(1))
{
PlaceBlock();
}
if (Input.GetMouseButtonDown(0))
{
}
}
}
private void PlaceBlock()
{
GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
}
}
Comment