Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by tadshirley0 · Jan 16, 2019 at 02:51 PM · spritespriterendererhealthbartiled

How to change the width of a Tiled sprite from script?

I'm making a Health Bar prefab (see screenshot). I use an image of a square that I repeat with Draw Mode set to Tiled to divide the bar into squares. I want enemies with more health to have longer bars with more squares. When I change the Width in the inspector, it works as expected (the bar appears longer or shorter and more squares are added or removed accordingly). However, when I change the width in code, nothing happens.

alt text

I tried setting SpriteRenderer.size but it doesn't do anything (the width and height of my SpriteRenderer don't change). The doc says Property to set/get the size to render when the SpriteRenderer.drawMode is set to SpriteDrawMode.Sliced. and in my case, the Draw Mode is not set to Sliced. Even if I set the DrawMode to Sliced (which isn't what I want) it doesn't do anything either. So how do I change the width of my Tiled Sprite through code?

I can't use SpriteRenderer.transform.localScale because that would stretch the bar without adding more squares.

EDIT: The problem was that I was using mySpriteRenderer.size.Set(widht, height); I used mySpriteRenderer.size = new Vector2(width, height); instead and it worked.

untitled.png (84.1 kB)
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

2 Replies

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

Answer by xxmariofer · Jan 16, 2019 at 03:04 PM

Just did this simple script for making sure it workded and everything seems fine, Just create a SpriteRenderer object and copy that lines in start, then just play with the vector2 vars for adjusting it to you gameplay mechanic.

 void Start()
 {
      spriteRenderer = GetComponent<SpriteRenderer>();
      spriteRenderer.drawMode = SpriteDrawMode.Tiled;
      spriteRenderer.size = new Vector2(5,1);
 }
Comment
Add comment · Show 1 · 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 tadshirley0 · Jan 16, 2019 at 03:16 PM 0
Share

Thank you, I was using mySpriteRenderer.size.Set(widht, height); I used mySpriteRenderer.size = new Vector2(width, height); ins$$anonymous$$d and it worked

avatar image
6

Answer by tormentoarmagedoom · Jan 16, 2019 at 03:01 PM

Good day.

How do you try to change it?

Spriterenderer size is a Vector2 variable, you should be able to change it as the documentation says.

https://docs.unity3d.com/ScriptReference/SpriteRenderer-size.html

 sprRend.size += new Vector2(0.05f, 0.01f);

or

 sprRend.size = new Vector2(3, 1);

Bye!

Comment
Add comment · Show 1 · 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 tadshirley0 · Jan 16, 2019 at 03:15 PM 0
Share

Thank you, I was using mySpriteRenderer.size.Set(widht, height) I used mySpriteRenderer.size = new Vector2(width, height) ins$$anonymous$$d and it worked

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

129 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

Related Questions

White lines between adjacent/overlapping sprites and from tiled sprites? 0 Answers

hide animated Sprite by code (SpriteRenderer) 2 Answers

Script to change SpriteRenderers sprite field 2 Answers

Override sprite geometry of sprite generated at runtime 1 Answer

[Unity3D] Sprite not rendering correctly when it's too small? (Appearing like a blob) 0 Answers


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