Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Faniha · May 17, 2015 at 08:22 AM · c#2dspritechangeinteger

changing sprites depending on int value

I have an object in my 2d game and I want the image rendered to differ depending on the value of an int (curHealth).

 public class PlayerHealth : MonoBehaviour {
 
     public int curHealth = 3;
     
     private SpriteRenderer myRenderer;
 
     public Sprite[] CookieLife;
 
     void start (){
         myRenderer = gameObject.GetComponent<SpriteRenderer>();
         if (curHealth == 6) {
             myRenderer.sprite = CookieLife[5];
         }
         if (curHealth == 5) {
             myRenderer.sprite = CookieLife[4];
         }
         if (curHealth == 4) {
             myRenderer.sprite = CookieLife[3];
         }
         if (curHealth == 3) {
             myRenderer.sprite = CookieLife[2];
         }
         if (curHealth == 2) {
             myRenderer.sprite = CookieLife[1];
         }
         if (curHealth == 1) {
             myRenderer.sprite = CookieLife[0];
         }
         if (curHealth == 0) {
             //destroy object and end game
 
         }
     }
 }
 

I added the images (whom are from a sprite sheet) to the list through the inspector.

I am new to programming so looked around for what other people have done an made this piece of code, but I cannot get it to work - the console doesn't show any errors, so I really have no idea what is wrong.

I looked at the following questions to get inspiration: http://answers.unity3d.com/questions/578228/change-texture2d-of-sprite-within-sprite-renderer.html and http://forum.unity3d.com/threads/mini-tutorial-on-changing-sprite-on-runtime.212619/

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 $$anonymous$$ · May 17, 2015 at 12:00 PM

Your problem lies in the fact your running your code in Start instead of Update.

The start function executes once when you object "starts" up. Update executes once per frame.

Try changing the line:

 void start (){

to

 void Update(){


http://answers.unity3d.com/questions/10189/what-is-the-general-use-of-awake-start-update-fixe.html

Comment
Add comment · Show 23 · 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 $$anonymous$$ · May 17, 2015 at 12:01 PM 0
Share

Also using GetComponent is an expensive operation so you don't want to run this every frame! $$anonymous$$ake sure to do this in the start function and store the result in a variable

avatar image Faniha · May 17, 2015 at 12:08 PM 0
Share

thanks :) it works now

avatar image $$anonymous$$ · May 17, 2015 at 12:51 PM 1
Share

And a small optimization that may or may not apply:

You are using a variable health where the values can be: 6, 5, 4, 3, 2, 1, 0.

The indexes for your sprites are 5, 4, 3, 2, 1, 0.

Note that both are a consecutive list of numbers and the indexes are one less than the health (except when the hp is 0);

So you can change your code to be:

 public void ChangeSprite()
 {
     if(curHealth == 0)
     {
         // Destroy gameObject here
     }
     else
     {
         // Change the sprite index to be one less than your health.
         myRenderer.sprite = CookieLife[curHealth - 1];
     }
 }

Sorry if that got complicated. I know your new at this...

avatar image $$anonymous$$ · May 17, 2015 at 01:24 PM 2
Share

Wait Start needs to have a capital

avatar image $$anonymous$$ · May 17, 2015 at 01:33 PM 1
Share

@barbe63: Thanks

@Faniha: Glad we could help

Show more comments

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

19 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

Related Questions

cutout animation in top down view 2d 1 Answer

Strange artifacts in builds 0 Answers

Texturing custom 2d sprite 0 Answers

how do I change a 2D Sprite Character Image/Graphic to another Sprite Character Image/Graphic with the same movements, animation,mechanim etc. 2 Answers

Spawning sprite in for loop 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