Why aren't my prefab clones destroying on play mode exit?
I'm instantiated some clones based off a prefab in my Resources folder when Start() is called in one of my other scripts. It instantiates fine, but when I exit, the clones persist, and if I click play again, they exist in the new run along with the newer clones. This only is happening for one prefab's clones, my other prefab clones are destroying automatically when I exit play mode.
private IDable stageMarkerTemplate;
void SetUpMarkerLines() {
stageMarkerTemplate = Resources.Load<IDable>("Prefabs/Marker Line");
float xDist = GlobalVars.horizSize/GlobalVars.horizSecs;
for (int i = 1; i < GlobalVars.horizSecs; i++) {
SegmentHelper.snapLines.Add(new LineRepr(
-1 * GlobalVars.horizSize/2.0f + xDist * i));
Instantiate(stageMarkerTemplate,
new Vector3(-1 * GlobalVars.horizSize/2.0f + xDist * i + 1.5f,
GlobalVars.markerLineY,
0),
Quaternion.Euler(0, 0, 0));
}
}
void Start() {
...
SetUpMarkerLines();
...
}
The stageMarkerTemplate is the prefab that is being cloned. Anyone have any idea why this is happening and how I can change my code to have these clones automatically destroy when play mode is exited?
Answer by aryali97 · Jun 02, 2019 at 06:14 PM
Think I figured it out, Start() is called again when play mode is exited to possibly set up the scene when editing the scene which reinstantiates the marker lines. I avoided the issue by changing my Start() to
void Start() {
if (Application.isPlaying) {
SetUpMarkerLines();
}
}
Your answer
Follow this Question
Related Questions
How to destroy the most ancient clone? 0 Answers
Destroying a prefab bullet after time with the list 0 Answers
GameObject.Destroy(gameObject); Not Working? 0 Answers
Third person controller moving backwards when shooting 0 Answers
Destroy specific clone 3 Answers