Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by JerinDd · Jun 30, 2020 at 06:53 PM · user interfaceicons

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?

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 CodesCove · Jun 30, 2020 at 10:09 PM 0
Share

Pressing E does nothing in your code.. did you mean pressing space?

1 Reply

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

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.

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 JerinDd · Jul 02, 2020 at 05:54 PM 0
Share

You are a good person, thank you.

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

133 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

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

UI buttons become unselectable on scene change 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