10. How to make a 2D Platformer Script Error [SOLVED]

Updated on December 3, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on November 29, 2018

I realize this tutorial is 4 years old now, but I was hoping this issue could still be resolved. In creating the script to kill the player, I have encountered an error (in c#):

 

 

 

 

 

 
using UnityEngine;
using System.Collections;
 
public class Player : MonoBehaviour {
 
public class PlayerStats
 {
 public int Health = 100;
 }
 
public PlayerStats playerStats = new PlayerStats();
 public void DamagePlayer (int damage)
 {
 
playerStats.Health -= damage;
 if (playerStats.Health <= 0)
 {
 GameMaster.KillPlayer(this); //"GameMaster.KillPlayer" is where the error appears
 }
 
}
}
 

 

 

 

 

 

Severity Code Description Project File Line Suppression State

 

Error CS0120 An object reference is required for the non-static field, method, or property ‘GameMaster.KillPlayer(Player)’ 2D Platformer.CSharp C:\Users\Public\Documents\Unity\2D Platformer\Assets\Player.cs 19 Active

 

 

 

Any help in fixing this would be greatly appreciated, as I’m rather lost myself. I could not find a similar issue, sorry if this question has been asked already.

  • Liked by
Reply
1 on November 29, 2018

KillPlayer needs to be a static method to call it like this.

Otherwise, you need a reference to a GameMaster object.

 

on December 3, 2018

Thank you, that was indeed the problem!

Show more replies
  • Liked by
Reply
Cancel