• 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
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
JadsonAlmeida

People who like this

1 Show 0
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
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
tadshirley0
EDevJogos
lslu
JadsonAlmeida
PoRtCuLLiS

People who like this

5 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); instead and it worked

avatar image

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
tadshirley0
xxmariofer
craftingtycoon
armagedoomgamesstudio
casaldejovesqueix
JadsonAlmeida

People who like this

6 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) instead 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Very weird pixel displacement with point filter mode 2 Answers

How to access an objects SpriteRenderer 2 Answers

Allow 2D sprite to receive light from any direction and show on both sides 0 Answers

Set Gameobject a certain color based off of other game objects 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