Hello!
I’ve been pondering a bit and I can’t quite get this to work using unity components.
I apologize for the long text, but I hope some of you gurus still can read through and suggest something to help me
My problem is:
I have a list of player characters. This list contains a number of class instances, which (simplified) look something like this:
public class Character
{
public GameStatics.CharacterClass characterClass;
public string name;
public int health;
public int strength;
}
I add the class to a static list which I keep into a static class like so:
GS.characterList.Add(new Character());
So, I have a list of the characters easily available, and when I start the game, I can select character by setting the index into the list, and I can use this inside the level like this:
GS.characterList[GS.playerIndex].health = 20;
And here’s another practical thing:
I can create a new class instance at the start, like so:
Character player = new Character();
player = GS.characterList[GS.playerIndex];
So, I can then write:
player.health = 20
and the other class instance will also get the change.
So, you can see this is all good, except one problem:
However, since I don’t inherit from monobehaviour, I don’t see the values in the inspector. I would really like to inspect, and adjust the Character fields easily.
So I wonder, what’s the best way of doing what I described above using a monobehaviour?
I tried to setup the character class to inherit from monobehaviour, but those can’t be instanciated using new keyword.
They must be instanciated using AddComponent(). So I need to create a gameobject, add the component, and add the gameobject to my static list.
I thought I could get to the content of the static list like so:
Character player = GS.characterList[GS.playerIndex].GetComponent<Character>();
and I can then reference it’s fields like I did before:
player.health = 20;
But “player” ends up as a null, so it looks like the objects inside my static list are destroyed as I load the level.
I thought a static list should keep it’s contents also after level load?
So, what to do?
Kjetil