- Home /
Looking to destroy clone of gameobject without destroying original
Basic idea: Crate prefab spawns.
If you double click on the crate, it instantiates a larger version of that, a sort of 'zoom in' on it, to show details.
If you then click outside of the panel, it destroys the instantiated clone, as well as hiding the UI panel that has the other details on it.
Currently this destroys both the clone and the original, which isn't what I want, I just want to destroy that clone.
if (Input.GetMouseButtonDown(0))
{
float timeSinceLastClick = Time.time - lastClickTime;
Ray mouseRay = GenerateMouseRay();
RaycastHit hit;
if (Physics.Raycast(mouseRay.origin, mouseRay.direction, out hit))
{
if (hit.transform.tag == "crate")
{
if (timeSinceLastClick < clickThreshold)
{
doubleClick = true;
Debug.Log("Double click");
Debug.Log(timeSinceLastClick);
crateHighlighted = true;
currentCrate = hit.transform.gameObject;
ToggleHighlight(currentCrate);
Time.timeScale = 0;
}
lastClickTime = Time.time;
//currentCrate set to gameObject that was hit
currentCrate = hit.transform.gameObject;
}
else
{
if (crateHighlighted)
{
ResumeTime();
}
}
}
}
void ToggleHighlight(GameObject selectedPackage)
{
highlightedCrateTemp = selectedPackage;
Debug.Log(highlightedCrateTemp.GetComponent<Crate>().itemName);
GameObject highlightedPackage = Instantiate(highlightedCrateTemp, highlightedCratePos, highlightedCrateTemp.transform.rotation);
Destroy(highlightedPackage.GetComponent<Rigidbody>());
uiManager.DisplayHighlightedDetails(highlightedCrateTemp);
}
void ResumeTime()
{
Time.timeScale = 1;
uiManager.highlightedObjectDetails.SetActive(false);
highlightedCrateTemp.SetActive(false);
Destroy(highlightedCrateTemp);
}
Answer by aaron4202 · Oct 12, 2021 at 11:55 PM
It looks like you are setting your crate to be temp and you are instantiating it under a local variable and then destroying your temp variable. You will use your function variable to define what will be instantiated not your temp variable and we will make your temp variable your clone So....
Remove your line
GameObject highlightedCrateTemp = selectedPackage;
under your ToggleHighlight function.
And change
GameObject highlightedPackage = Instantiate(highlightedCrateTemp, highlightedCratePos, highlightedCrateTemp.transform.rotation);
to
GameObject highlightedCrateTemp = Instantiate(selectedPackage, highlightedCratePos, highlightedCrateTemp.transform.rotation);
And it should do the trick.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044116im_/https://answers.unity.com/themes/thub/images/avi.jpg)