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;
}
}
}
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.
Answer by WingToh · Aug 20, 2019 at 12:07 PM
Restarting Unity seemed to have solved the issue for me.