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 /
  • Help Room /
avatar image
0
Question by WingToh · Aug 19, 2019 at 04:56 PM · fpsimagebeginnerresizing

Can't resize image properly in script? Need help

Im new to Unity and need help with resizing images. Ive searched for long on internet but I can't find an answer for this:

I created a FPS where the Player has an Ultimate Charge. Ultimate Charge charges from 0% - 100% depending on how much damage the player deals to enemies. If Ultimate Charge reaches 100% you can use an "ultimate ability" and the Ultimate Charge turns back to 0%.

I have a UI image displaying my Ultimate Charge. I want this image to become bigger when the Ultimate Charge reaches 100% and get smaller again when it is 0% (when i use the ultimate ability). So, I tried this with sizeDelta and it only works oneway: the image becomes bigger at UltimateCharge = 100% but afterwards it does not become smaller again when it is 0-99%. help please!

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UltimateImage : MonoBehaviour {

 //scripts
 public PlayerShoot pS;

 //sprites
 public Sprite ultimateNotCharged;
 public Sprite ultimateCharged;

   
 void Start()
 {
     
 }

   
 void Update()
 {
     changeImage();
 }

 void changeImage()
 {
     float ultimateCharge= pS.ultimateCharge;
     var ultImageTransform = transform as RectTransform;
     if (ultimateCharge >= 100f)
     {
         ultImageTransform.sizeDelta = new Vector2(140, 157);
         GetComponent<Image>().sprite = ultimateCharged;
     }
     if (ultimateCharge<100)
     {
         ultImageTransform.sizeDelta = new Vector2(105, 118);
         GetComponent<Image>().sprite = ultimateNotCharged;
     }
 }

}

Comment
Add comment · Show 1
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 WingToh · Aug 19, 2019 at 05:21 PM 0
Share

The image DOES become bigger and smaller (works) if i remove both GetComponent().sprite = ultimateCharged; and GetComponent().sprite = ultimateNotCharged;

But I dont want to remove these as I want to show different images when Ultimate Charge is 100% and when it is not.

I dont understand why this happens please help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by WingToh · Aug 20, 2019 at 12:07 PM

Restarting Unity seemed to have solved the issue for me.

Comment
Add comment · 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

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

228 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

What do I need to do to develop an FPS survival game? 1 Answer

My raycasting isn't working 1 Answer

cursor disappears in GameOver scene 0 Answers

I want my button to give the player randomly 1 of 5 images after clicked. How do i do this? 0 Answers

First person shooter : Leaning! 2 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