- Home /
Why won't my UI script work?
So what i'm trying to do is making an icon change sprites when something happens, yes is the UI image component, uno is the first sprite and dos is the second, OK is just to make sure you cant spam E and make the icon blink like crazy.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class TelIcon : MonoBehaviour { Image yes; public Sprite uno; public Sprite dos; public bool OK;
// Start is called before the first frame update
void Start()
{
yes = GetComponent<Image>();
OK = true;
}
// Update is called once per frame
void FixedUpdate()
{
if(OK == true){
if(Input.GetKey(KeyCode.Space)) {
StartCoroutine(hmm());
OK = false;
}
}
}
public IEnumerator hmm(){
yes.sprite = dos;
yield return new WaitForSeconds(1f);
yes.sprite = uno;
OK = true;
}
}
Instead of changing to the second icon after pressing E, then changing back after 1 second, it just stays as the first icon and doesn't change what so ever. Can anyone help me with this please?
Pressing E does nothing in your code.. did you mean pressing space?
Answer by haizathaneefa · Jul 01, 2020 at 04:15 AM
I've made a script that suit what you want to do. Just copy and try it out in your scene
public Sprite[] sprite; // sprites that will be used for an image
bool OK;
public Image image; // the image that you want to change
void Start()
{
OK = true;
image.sprite = sprite[0]; // set the image sprite initially
}
void Update()
{
if (OK && Input.GetKeyDown(KeyCode.E)) // if OK bool is true and E press
{
StartCoroutine(ChangeSprite());
OK = false;
}
}
IEnumerator ChangeSprite()
{
image.sprite = sprite[1];
yield return new WaitForSeconds(1f);
image.sprite = sprite[0];
OK = true;
}
Hope this works for you.
Your answer
Follow this Question
Related Questions
how to import icon in publisher account 0 Answers
How to hide different team icons 0 Answers
Unity 3d in touch sensitive webstore interface. 2 Answers
UI Text Small on screen device. 1 Answer