I am working on a small system for my game, I want to be able to have a central registry to store base " objects " to spawn in my game, I want to then have a data which is universally used across all items, this is SpawnableData, this has " parameter " structs which I can serialize into an XML, the problem I am having is, whenever I make a new instance of my spawnable class, for some reason, its data acts like a struct. whenever I edit one of them, it automatically edits every other one in the scene. I have tried Instantiating new instances and I have also tried building new gameobjects, attaching components and assigning data manually, whatever I do, it always seems to reference its data to one central source, which I am confused with.
Any help would be greatly appreciated with this issue, I have tried multiple times with different ways to try and stop this but for some reason, the data is always being referenced from the same place, no matter what I do.
Here are the classes I am using which are related to the issue :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawner : MonoBehaviour
{
public int spawnableID;
public SpawnableData data;
public bool customData;
void Start()
{
spawn();
spawn();
}
public Spawnable spawn()
{
GameObject obj = new GameObject();
Spawnable inst = obj.AddComponent<Spawnable>();
if (customData)
{
Parameter[] parameters = new Parameter[data.parameters.Length];
parameters = data.parameters;
inst.data = new SpawnableData();
inst.data.parameters = parameters;
}
return inst;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SpawnableRegistry : MonoBehaviour
{
static Spawnable[] registry;
void Awake()
{
populateRegistry();
}
public static Spawnable getSpawnable(int spawnableID)
{
return registry[spawnableID];
}
void populateRegistry()
{
registry = Resources.LoadAll<Spawnable>("Spawnable");
}
}
using UnityEngine;
using System.Collections;
[System.Serializable]
public struct Parameter
{
public string data;
public DataType dataType;
}
using UnityEngine;
using UnityEditor;
using System.Collections;
[System.Serializable]
public struct SpawnableData
{
public Parameter[] parameters;
}
using UnityEngine;
using System.Collections;
public class Spawnable : MonoBehaviour
{
public int spawnableID;
public SpawnableData data;
public virtual SpawnableData save()
{
return data;
}
public virtual void load(SpawnableData loadedData)
{
data = loadedData;
}
// To test the data
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
for(int i = 0; i < data.parameters.Length; i++)
{
Debug.Log(data.parameters*.data);*
}
}
}
}