- Home /
Instantiated Prefab doesnt destroy
really odd issue.
Im Instantiating a dice prefab and then destroying the prefab and re instantiating to roll again with OnGUI button click.
the main issue is that after the 2nd destroy/instantiation the prefabs are no longer destroyed or registered.
code is -
void DieSpawn()
{
if (GameObject.FindGameObjectWithTag("Die"))
{
//GameObject.Destroy(dieGameObject);
Destroy(GameObject.Find(dieName));
//dieGameObject = null;
}
// create the die prefab/gameObject
dieGameObject = (GameObject)Instantiate(diePrefab,spawnPoint.transform.position ,Quaternion.identity)as GameObject;
// give it a random rotation
dieGameObject.transform.Rotate(new Vector3(Random.value * 300, Random.value * 300, Random.value * 300));
//create reference to gameobject
dieGameObject = GameObject.Find(dieName);
dieValueComponent = dieGameObject.GetComponent<DieValue>();
//apply force
dieGameObject.constantForce.force = new Vector3(1, 0, 1);
}
ideas as I just cant see whats wrong with this...
Answer by Hybrid1969 · Oct 17, 2013 at 11:08 PM
solved the issue.
just found the solution while looking at another script for answers to another problem.
changed - Destroy(dieGameObject);
to - DestroyImmediate(dieGameObject);
and problem solved
Was it this one? :D
Be careful with DestroyImmediate. It can permanently destroy assets!
Just noticed there's a whole answer and a bunch of comments missing from this page.... they've just gone. $$anonymous$$y comment was
The object won't be destroyed until next frame so repopulating the variable-object you just destroyed may cause problems.
thanks for putting your original comment back...I made a bit of mess of this questions.
DestroyImmediate currently seems to be the only solution currently
I had to resort to it after I had a similar problem between Destroying and Instantiating in the same frame.
It works but be careful with it. DestroyImmediate actually comes with a warning in the scripting reference. Besides asset destruction it can cause problems with NullRefs etc.
Answer by chillersanim · Oct 17, 2013 at 09:39 PM
When instantiate an object, Unity adds the string "(clone)" behind his name.
If you'r property "dieName" is only the original name (without "(clone)" ), then it will not find any gameobject on the line 8 of your code.
Instead, you can rename the instantiated object so that the "(clone)" is removed -> the gameobject is being found:
dieGameObject.Name.Replace("(Clone)", string.Empty);
I'm not sure why you have added the line 20, because the dieGameObject is already referencing the desired object.
If it is NOT because of the name, then there are other possibilities what could have gone wrong
The prefab doesn't have the correct tag
Some other script manipulates the tag or name of the object
At least, I would reccomend you, not to use different methods to find an object (like GameObject.FindGameObjectWithTag("Die") and GameObject.Find(dieName))
Then you can at least be sure, that the problem is with the choosen method.
You can also debug to check if it enters the if on line 4, and if it finally finds the dieObject.
Hope I was able to help
Greetings
Chillersanim