- Home /
Function not deleting game objects after leaving play mode.
I am creating a custom UI control and the setting up a custom inspector. The inspector has a function to add or remove sliders. The sliders are stored in a list when they are created. It all works fine before I enter play mode. Once I have entered and then left play mode it no longer deletes the game objects and it creates a new game object with no name, no parent and no recttransform. It does remove them from the list but they are not deleted. Even odder if I call the SetPadding function all by itself it creates the same type of game object. I have a feeling it has to do with the serialization but I am super lost.
private void ManageSliders()
{
if (dccNumberAllotment.numberOfFields < dccNumberAllotment.slidersList.Count)
{
Debug.Log("Test the less than managesliders");
int differnce = dccNumberAllotment.slidersList.Count - dccNumberAllotment.numberOfFields;
for (int i = 0; i < differnce; i++)
{
Debug.Log("Test the less than managesliders for loop");
NumberAllotmentSlider slider = dccNumberAllotment.slidersList[dccNumberAllotment.slidersList.Count - 1];
DestroyImmediate(slider.NumberAllotmentSliderObject);
dccNumberAllotment.slidersList.Remove(slider);
}
}
if (dccNumberAllotment.numberOfFields > dccNumberAllotment.slidersList.Count)
{
int differnce = dccNumberAllotment.numberOfFields - dccNumberAllotment.slidersList.Count;
Debug.Log("Test the more than managesliders");
for (int i = 0; i < differnce; i++)
{
Debug.Log("Test the more than managesliders for loop");
NumberAllotmentSlider slider = dccNumberAllotment.slidersList[dccNumberAllotment.slidersList.Count - 1];
EditorUtility.SetDirty(slider.NumberAllotmentSliderObject);
RectTransform rect = slider.NumberAllotmentSliderObject.GetComponent<RectTransform>();
NumberAllotmentSlider sliderComponent = new NumberAllotmentSlider(dccNumberAllotment.content, "Slider " + (dccNumberAllotment.slidersList.Count + 1), Slider.Direction.LeftToRight,
new Vector2(.5f, 1), new Vector2(.5f, 1), new Vector2(.5f, 1), 0, rect.anchoredPosition.y - rect.rect.height, 160, 20);
sliderComponent.slider.wholeNumbers = true;
dccNumberAllotment.slidersList.Add(sliderComponent);
}
}
SetPadding();
}
private void SetPadding()
{
Debug.Log("Test SetPadding");
Debug.Log("Sliders List Count; " + dccNumberAllotment.slidersList.Count);
// Debug.Log("LastPadding from start of SetPadding: " + lastPadding);
for (int i = 0; i < dccNumberAllotment.slidersList.Count; i++)
{
Debug.Log("Test SetPadding for loop");
if (i > 0)
{
RectTransform topRect = dccNumberAllotment.slidersList[i - 1].NumberAllotmentSliderObject.GetComponent<RectTransform>();
RectTransform bottomRect = dccNumberAllotment.slidersList[i].NumberAllotmentSliderObject.GetComponent<RectTransform>();
float currentPadding = (topRect.anchoredPosition.y - bottomRect.anchoredPosition.y) - bottomRect.rect.height;
if (currentPadding > dccNumberAllotment.padding)
{
float difference = dccNumberAllotment.padding - currentPadding;
RectTransform rect = dccNumberAllotment.slidersList[i].NumberAllotmentSliderObject.GetComponent<RectTransform>();
rect.anchoredPosition = new Vector2(rect.anchoredPosition.x, rect.anchoredPosition.y - difference);
}
if (currentPadding < dccNumberAllotment.padding)
{
float difference = dccNumberAllotment.padding - currentPadding;
bottomRect.anchoredPosition = new Vector2(bottomRect.anchoredPosition.x, bottomRect.anchoredPosition.y - difference);
}
}
}
}
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
How to Destroy Game Object In All Clients in Photon Network 0 Answers
Destroy all GameObjects EXCEPT some... 2 Answers
Why won't my script load? 1 Answer
Add and destroy instantiated gameobject in linkedlist C# 1 Answer