- Home /
The question is answered, right answer was accepted
How can I continue to instantiate an object after deleting
I am currently instantiating an object and adding it to a list using this code
for (int i = 0; i < 1; i++)
{
targets.Add(Instantiate(target, new Vector3(Random.Range(-10, 10), Random.Range(-5, 5), Random.Range(-10, -10)), transform.rotation) as GameObject);
}
and I am deleting it when clicked on using this function
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
SphereCollider bc = hit.collider as SphereCollider;
if (bc != null)
{
Destroy(bc.gameObject);
if (currentHealth <= 9)
{
currentHealth++;
}
}
}
}
You can ignore the health part. This all works however if I delete the first spawn there is nothing to instantiate, I want to continue to spawn those objects indefinitely while still retaining the ability to delete when clicked on.
Am I going about this the wrong way? Is there a proper way to do what I want or is there something in this code that can be fixed to accomplish the goal?
I do now and it works, but I have to click on the original twice to destroy it while all the other instantiated prefabs only require one click. Is this normally a thing?
I cant believe I forgot all about prefabs, I have been away from this for too long.
well, don't forget to remove the original from the hierarchy, it might be a duplicate and you're actually destroying 2 objects on top of each others.
Follow this Question
Related Questions
Best way for a shotgun fire? 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers
Multiple Cars not working 1 Answer
AI Instantiate targets/prefab. (pick target issue) 1 Answer
Grid Placement In Game 2 Answers