Help with destroying Clone of Instantiated
I have a building; both place-able and destructible by the player. The player places building by clicking button on UI(in HUD) which brings up a set of buttons(in HUD); each representing a building the player could build. when player selects building and places it, a clone is placed in world and created in hierarchy. After the building is placed player can hit Delete key to bring up UI button(in WorldSpace attached directly to building) on each clone that allows player to delete that instance of the clone when UI button is clicked.
The problem is that after player uses the Demolish button for the first time to delete the building, the player is no longer able to build the building; as the prefab does not appear on creation of further clones, although according to the hierarchy at runtime the Clone Object was the one deleted, not the prefab.
However, if i set the prefab to active so that it appears in the scene from Start and do not delete that instance of the gameObject than i can build after destroying; except instead of each of the clones retaining their own attributes, they all share the attributes of the prefab. for example, all buildings upgrade at the same time as opposed to on their own stats; as is what happens when the prefab is not active at runtime.
This process involves linking together 7 different scripts. 3 controlling the placing of the buildings. 1 that controls the stats and links to the UI/HUD, Prefabs, and other scripts. 2 that controls the visuals and UI for the player getting stats on individual buildings, and another that puts the demolish button on individual buildings when player pushes 'delete'.
Im no sure what part of the code to share here exactly but ill do my best.
Script(partial) Attached to building:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Example : MonoBehaviour
{
[SerializeField]
private Transform canvas;
public GameObject demolishButton;
}
void Awake()
{
demolishButton.gameObject.SetActive(false);
}
public void OnClickStats()
{
canvas.gameObject.SetActive(true);
Debug.Log("StatsAppear");
}
public void OffOnClickStats()
{
canvas.gameObject.SetActive(false);
Debug.Log("StatsDisappear");
}
public void DeleteBuilding()
{
Destroy(gameObject);
Debug.Log("Destroy Building");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Delete))
{
if (demolishButton.gameObject.activeInHierarchy == false)
{
demolishButton.gameObject.SetActive(true);
Debug.Log("Destroy Building Mode On");
}
else
{
demolishButton.gameObject.SetActive(false);
Debug.Log("Destroy Building Mode Off");
}
}
The demolishButton is set to DeleteBuilding() on click in the inspector.
Script for building placement. Attached to camera: are based on Sebastian Lague's building placement tutorial.
https://www.youtube.com/watch?v=OuqThz4Zc9
Please let me know if you could use more info, or would like help replicating a part of my project in your own work.
Thank you in advance.
Your answer
