I have a list of components and I want to give it some values like so.
public LoadButtonData loadButtonData;
GameObject clone = Instantiate(buttonPrefab, transform);
LoadButtonProperties properties = clone.GetComponent<LoadButtonProperties>();
properties.filePath = filePath;
properties.Date.text = time.ToShortDateString();
properties.Name.text = fileName;
properties.Place.text = DataLoaded.sceneName;
properties.Thumbnail.sprite = newSprite;
loadButtonData.loadButtons.Add(properties);
LoadButtonData
is an object contained within another object with DontDestroyOnLoad() and is used across multiple scenes. However, after switching scenes, this happens:
I understand that this is because the properties are added into the list as references and upon changing scene, the button clones are destroyed and can't be referenced anymore.
Is there a workaround for this, for me to reuse my values throughout my scenes as actual values and not references? How do I add copies of the clone's LoadButtonProperties
data instead of references to the clone?
Edit: Rephrased the question above for clarification
Edit: This is the LoadButtonProperties class
public class LoadButtonProperties : MonoBehaviour
{
/// <summary>
/// Path to the json file for loading
/// </summary>
public string filePath;
[Tooltip("Place Date here")]
public TextMeshProUGUI Date;
[Tooltip("Place Name here")]
public TextMeshProUGUI Name;
[Tooltip("Place Place here")]
public TextMeshProUGUI Place;
[Tooltip("Place Thumbnail here")]
public Image Thumbnail;
}
DeepCloning was attempted but I couldn't manage to do it because of the Image
variable
The only way to keep those references "alive" is to prevent those objects from being destroyed in the first place (which isn't viable when dealing with scene changes).
Keeping..."a hold" of the object while it is removed from memory and recreated isn't really possible. You might be able to set up some weird serialization/deserialization step, but you'd have to make your list some kind of UUID that when you change scenes, the objects figure out their UUID and the list queries the scene for objects with that UUID, but...it's going to be messy. And might not even work all the time.
You're better off saying "this data is volatile" and building with that in mind. Just let things expire and recreate (if neccessary) them when the scene changes.