Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jimm84 · Sep 13, 2019 at 03:09 PM · sprite2d gamespriterendererspritesheethealth

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jimm84 · Sep 17, 2019 at 09:17 PM 0
Share

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!

avatar image Kristifor_p jimm84 · Sep 17, 2019 at 10:07 PM 0
Share

No problem i'm glad that i have helped you out!

avatar image jimm84 Kristifor_p · Sep 19, 2019 at 05:48 PM 0
Share

Thanks! Will help for the future!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to choose a size/resolution for the sprites used as game assets in Unity2D ? 2 Answers

How to truncate sprite without changing the scale of the sprite? 1 Answer

How to bring empty game object in front of other Panels? 1 Answer

Override sprite geometry of sprite generated at runtime 1 Answer

How to Detect Which Sprite a Sprite Renderer Is Using? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges