Hey
I have a list of characters where I store character stats:
Character playerInList = GS.Instance.characterList[GS.playerIndex].GetComponent<Character>();
I have a bunch of these characters, and the component contains things like this:
public int Level = 1;
public int Health = 50;
public int Strength = 10;
To make this easier to access in the game, I instanciate the game object, like this:
playerGO = (GameObject)Instantiate(GS.Instance.characterList[GS.playerIndex]) as GameObject;
playerGO.name = "PlayerGO";
player = playerGO.GetComponent<Character>();
So, I get a copy of the fields, and it’s easy to manipulate in the game, using player.Health = 12;
However, when the player dies, or when the player exits, or has finished the level, I want to save the character back to the original one.
I hoped this would work:
charInList = player
but, it does not.
I know of two other possibilities:
- Just copying all the fields from player, into charInList, like so.
charInList.Health = player.Health.
But this can get somewhat complicated and error prone.
- Destroying charInList GameObject, and Instanciating player gameobject.
But if there are other better ways, it would be great to hear about it
Thanks a bunch!
Edit: Destroying the gameobject inside the list, and instanciating a copy from the player gameobject into the list again seems to work ok.
Is this fine to do?
Kjetil