- Home /
Issue with swapping Player sprite when health lowers - Not sure how to fix
PlayerStats.cs(30,52): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
hello all, I was hoping somebody may be able to help as I am a bit stuck. and please bear with me as I am design / art heavy. So I have got almost to the final stages of making the player health work but I have now hit a barrier when I want to change the player sprite based on the condition of the player, less 30, bit battered, less than 20, not great, 10 critical and so on.
I'm hoping there might be a simple fix but I can't find a way to do so.
Is there something I am missing to make this work correctly I do I need to go about it a different way? Ive drawn a blank!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // references the unity UI so you can tinker with it
public class PlayerStats : MonoBehaviour {
public GameObject ButtonRestart;
public float speed;
static int playerHealth = 40; // health points
public Sprite PlayerLooks4;
public Sprite PlayerLooks3;
//health-sprite_1;
int damage=25; // Debug visual of health shown
//public Text GameOverText;//Store a reference to the UI Text component which will display the 'You win' message.
// Update is called once per frame
void Start () {
print (playerHealth);
playerHealth = 40;
}
void Update (){
if (playerHealth < 30) {
print ("Bit Damaged" + playerHealth);
//this.gameObject.GetComponent<SpriteRenderer> ().sprite == PlayerLooks3; this is the issue line
}
}
// Contact pain --- when enemy touches player, it causes pain. have tag of "Enemy" on enemy
void OnCollisionEnter2D(Collision2D _collision){
if(_collision.gameObject.tag=="Enemy"){
playerHealth-=damage;
print ("Germ hurts! Ouch!" + playerHealth);
}
// if end
if (playerHealth < 0) {
print ("player dies");
Dies ();
}// second if end
}
public void Dies() {
ButtonRestart.SetActive (true);
speed = 0;
gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
// spawn Gibs
// Disable player keyboard moves
}
void OnTriggerEnter2D(Collider2D other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag ("healthPickup"))
{
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
//Add one to the current value of our count variable.
playerHealth = playerHealth + 4;
print ("Juicy 1 - cool!");
//Update the currently displayed count by calling the SetCountText function.
}
}
}
Thanks
Answer by Kristifor_p · Sep 15, 2019 at 08:06 PM
I got you, i have wrote a simple code for you i tested it and it worked.
You will need to include this script in your player and if you need some explanation of how it works let me know.
[Header("Sprite Settings:")]
public Sprite lowHealth; // Displaying sprite for low health.
public Sprite mediumHealth; // Displaying sprite for medium health.
public Sprite fullHealth; // Displaying sprite for full health.
[Space]
[Header("Current Health:")]
public float health = 100f; // This represent the current health.
private SpriteRenderer spriteRenderer;
private void Start()
{
// Grab your sprite renderer from your GameObject.
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
PlayerHealth();
}
private void PlayerHealth ()
{
if (health < 30f)
{
spriteRenderer.sprite = lowHealth;
}
else if (health < 60f)
{
spriteRenderer.sprite = mediumHealth;
}
else
{
spriteRenderer.sprite = fullHealth;
}
}
Hope this will help you out! @jimm84
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; // references the unity UI so you can tinker with it
public class PlayerStats : $$anonymous$$onoBehaviour {
public GameObject ButtonRestart;
public float speed;
//static int playerHealth = 40; // health points
[Header("Sprite Settings:")]
public Sprite lowHealth; // Displaying sprite for low health.
public Sprite mediumHealth; // Displaying sprite for medium health.
public Sprite notbadHealth; // Displaying sprite for near full health.
public Sprite fullHealth; // Displaying sprite for full health.
[Space]
[Header("Current Health:")]
public float health = 100f; // This represent the current health.
private SpriteRenderer spriteRenderer;
int damage=25; // Debug visual of health shown
//public Text GameOverText;//Store a reference to the UI Text component which will display the 'You win' message.
// Update is called once per frame
private void Start () {
print (health);
health = 100;
spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
playerHealth();
}
public void playerHealth ()
{
if (health < 10f)
{
spriteRenderer.sprite = lowHealth;
}
else if (health < 50f)
{
spriteRenderer.sprite = mediumHealth;
}
else if (health < 70f)
{
spriteRenderer.sprite = notbadHealth;
}
else
{
spriteRenderer.sprite = fullHealth;
}
}
// Contact pain --- when enemy touches player, it causes pain. have tag of "Enemy" on enemy
void OnCollisionEnter2D(Collision2D _collision){
if(_collision.gameObject.tag=="Enemy"){
health-=damage;
print ("Germ hurts! Ouch!" + health );
}
// if end
if (health < 0) {
print ("player dies");
Dies ();
}// second if end
}
public void Dies() {
ButtonRestart.SetActive (true);
speed = 0;
gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
// spawn Gibs
// Disable player keyboard moves
}
void OnTriggerEnter2D(Collider2D other)
{
//Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
if (other.gameObject.CompareTag ("healthPickup"))
{
//... then set the other object we just collided with to inactive.
other.gameObject.SetActive(false);
//Add one to the current value of our count variable.
health = health + 4;
print ("Juicy 1 - cool!");
//Update the currently displayed count by calling the SetCountText function.
}
}
}
Thanks, although I have amended slighly!
No problem i'm glad that i have helped you out!