Advice on a player script

Any pointers on the best way to create a new player after death.

My game design has five different characters. initially you have one and need to get coins in game to unlock the next one.

If you you unlock it before you die then it acts as a another life otherwise it is game over.

I’d appreciate any tips on the best way to script this in C#. Each ‘life’ is a unique character prefab.

Thinking off an list players.

player of players in the list and then adding one to int playerLives that is the counter for the list position.

On death playerlives++ if upgrade coin total is reached. hopefully get a chance to try this later any tips greatly appreciated.

So characters are like extra lives? So if you have characters A,B and C (A being the free one) and when you die while playing as C, you get a new live as B? and if you die again it goes to A?

In this case you could probably have an array for characters. Everytime you unlock a character set a bool value to true. When you die you could just check for highest available character by looping through the array.

1 Like

Yes that is the idea, although the char will be playable (unlocked) if the player has gained enough coins in play.

I am having problems with the array at the moment for some reason, the first player is working fine but I have a condition players == null which is not working, i think i need to add the bool you are suggesting.

You could give your basic player script a “NumberOfRespawns” integer variable.

You could preload this whatever number of respawns you want. In the 1980s we used three or four. :slight_smile:

Now when you die, do not destroy the player object, but rather do a sequence of steps like this:

  • disable control and movement of the player
  • hide the graphical portion (or make it fall to the ground animation-wise)
  • wait a period of time and play some death rattles
  • check the variable above (NumberOfRespawns)
  • if it is zero, go to game over
  • otherwise, resurrect the player, reenabling his controls, turning back on his graphics, etc.
1 Like

Awesome thanks -the only issue is the character is different each time.

To give a few more details it’s 5 different dragons who your are trying to get ‘home’.

You start with one and then the gameplay is to get enough coins to rescue the rest. Really you never want to see the other dragons (unless you pause and go to menu to switch once you have unlocked)

If your player dies then he cannot make it home ;-( but if you have other Dragons then they can take over the journey to get home.

Hope my rambling makes sense, I will have a coffer and post some snippets of code as I am having problems getting the next dragon to spawn. The first one works fine so the array is there.

I was wondering if its because i am using the same prefab currently while waiting for the other dragon art?

Keep your player script separate (in an empty gameObject) from the ‘character’ that is getting deleted when he dies - the player script can refer to the gameObject holding the character using a reference and so when it is deleted the reference is set to a new charater (new gameobject/script) and it doesn’t affect the code.

You could also attach the playerScript to the camera, and keep the player/character as a child of the camera

1 Like

Yes,

Argh, so I am deleting the script when I destroy the object. Thanks I will try that.

Yeah it’s a nasty (sometimes helpful) side effect of Destroy( GameObject ); it destroys all components of that gameObject including script, as well as any children currently attached under the GameObject set in the Unity hierarchy or through code.

Yes, there is a way to change an GameObject’s mesh during game, but the way Unity works I think it would be easier to keep the characters prefab separate from the script and just delete/create the prefabs when youre deleting/adding the new char and set a reference in your playerScript let’s say gobj_player and change that reference so it always refers to the ‘newest’ created character prefab.

How you determine which character is created is up to you - you could keep a running int charCount=0 in the playerScript and create a new char based on what value that is, but it really depends how your coding.

You could even create a reference of List in the playerScript that refers to the characters

List<GameObject> charTypes = new List<GameObject>();
int charCount = 0;
GameObject gobj_Player;
void Start()
{
     charTypes.Add (Resources.Load<GameObject>("Prefabs/char1"));     //charType[0]
     charTypes.Add (Resources.Load<GameObject>("Prefabs/char2"));    //charType[1]
     charTypes.Add (Resources.Load<GameObject>("Prefabs/char3"));    //charType[2]

    gobj_Player = GameObject.Find("charFirst"); //Create a 'first gameobject char in the scene,                                                                                   load the rest
    //Instead of finding one you could instantiate here (or set it in the inspector in the scene)
}

void loadNewChar()
{
         Destroy ( gobj_Player );
         gobj_Player = (GameObject)(Instantiate( charTypes[ charCount ] ) );
         gobj_Player.transform = transform; //Change new object to match position and rotation
         //if you want to make the gobj_Player a child of the playerScript/cam, do it here
         charCount++;
         //Add code here to change charCount if the value gets too high for charTypes list
}
1 Like

Thanks for that.

I am still struggling to implement currently but I have decide to go for an array as I know the number of players.

Is it possible to instantiate element one of array wait for that to be destroyed then instantiate element 2? checking first if the game has put a true element in there?

The array I made stores references to prefabs - which are not GameObjects that actually exist, but you can duplicate prefabs from the array using Instantiate - which will create a Gameobject duplicated of that prefab - so the List/Array is not the actual characters, but their prefab/type to be copied through Instantiate

And when you instantiate, the new GameObject created from the prefab data is stored in gobj_player in my code

1 Like

Sorry, I did try to reply the other day but for some reason my account was being funky and it had to be verified. I solved this via an array with a static int moving along it each time bool for dragons death was set to true.

Is only the first fix as I want to call other methods now to show death etc but it is a good starting point.

Cheers for all the advice :slight_smile: