- Home /
Not sure how to do this- destroy existing data in game obejct to prevent duplicates
Everything was working very well and I was getting excited to finally complete my project. Then I noticed that when I would open the same canvas(with a button) that I was on before, the data from the game object doubled, so I had two pieces of the same information. I would think to check if the gameobject exists, then destroy it. Only the temp variable I'm using for instatiation is local. I don't really want to use a global variable for it. Only when I ran a test, all my menu buttons disappeared- whoops. Anyone have some thoughts? I'll post code if needed.
public void ShowAllOilChanges()
{
for(int i = 0; i< OilChangeList.Count; i++)
{
GameObject ocObj = Instantiate(OilChangePrefab);
OilChange tmpOilChange = OilChangeList[i];
ocObj.GetComponent<OilChangeScript>().DisplayOilChanges("Date of Service: " +tmpOilChange.ServiceDate, "Place of Service: " +tmpOilChange.Location, "$$anonymous$$ileage: " +tmpOilChange.$$anonymous$$ileage, "Labor: " +tmpOilChange.Labor, "Oil Brand: " +tmpOilChange.OilBrand, "Oil Price: " +tmpOilChange.OilPrice, "Filter Brand: " +tmpOilChange.FilterBrand, "Filter Price: " +tmpOilChange.FilterPrice, "Purchase Place: " +tmpOilChange.OilFilterPurchaseLocation);
ocObj.transform.SetParent(OilDisplayparent);
}
}
Answer by Gilead7 · Nov 19, 2016 at 06:45 PM
I was thinking about this backwards. First, I made tags for each type of object to be destroyed, then added them on to their respective prefabs. This function is called by the window opening script upon closing and opening. Hope this helps someone like me. public void DestroyObjects(string tag) { GameObject[] Obj = GameObject.FindGameObjectsWithTag(tag); if(Obj==null) return; else { Debug.Log(Obj.Length); for(int i=0; i< Obj.Length; i++) { Destroy(Obj[i]); } Debug.Log(tag + "Objects Destroyed!"); }
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiating at random. Objects instantiates on top of eachother... 0 Answers
Saving data. 1 Answer
C# -- Populate dictionary automatically (not manually, yuck) 2 Answers