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
9
Question by Khorkhe · Sep 20, 2014 at 03:01 AM · uiunity 4.6

Unity 4.6 UI - change image's source image via script

Hi,

I have buttons for levels, that have an image component. I set the image using the editor to the default "level unlocked" image. Now in the script, I am looping through all the levels, and for each, I read a playerprefs string associated with that level to determine if it was unlocked. If the level was not unlocked, I want to change the source image of that component to a "level locked" image.

I tried to set the image name to my new image's name (clearly that failed atrociously)

 gameObject.transform.GetChild(j).GetComponent<Image>().sprite.name = "my_new_image";

I tried having for each level two buttons, one representing 'locked' and one representing 'unlocked', disabling one and enabling the other. That worked, but I really am not liking the style to do it, and would prefer a solution that would fix the first approach.

Thanks a lot

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

4 Replies

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

Answer by Zephrim · Sep 20, 2014 at 04:50 AM

What's wrong?

You need to be changing the actual Sprite Object, rather the the Sprite Object's name. The Sprite is the actual "Image" of the Image UI Component in Unity 4.6.

How do I fix it?

While I can't exactly talk you through it word for word, I can give you my example with comments.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TestMenu : MonoBehaviour 
 {
     /// <summary>
     /// This is the Sprite we're going to swap to.
     /// </summary>
     /// <remarks>
     /// This is set in Unity's Inspector.
     /// </remarks>
     public Sprite OtherSprite;
 
     /// <summary>
     /// The Array of Images.
     /// </summary>
     Image[] images;
 
     // Use this for initialization
     void Start() 
     {
         // Get all components of type Image that are children of this GameObject.
         images = gameObject.GetComponentsInChildren<Image>();
 
         // Loop through each image and set it's Sprite to the other Sprite.
         foreach (Image image in images)
         {
             image.sprite = OtherSprite;
         }
     }
 }
Comment
Add comment · Show 8 · 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 Khorkhe · Sep 20, 2014 at 07:39 PM 0
Share

Hi, i ended up using part of your solution since the GetComponentsInChildren wasn't my main problem. But having a public Sprite[] that contains the images I want (i initially set them in the inspector) worked beautifully. Thanks!

avatar image Zephrim · Sep 21, 2014 at 12:07 AM 0
Share

Glad I was able to help you!

avatar image Carrel · Mar 16, 2015 at 06:54 PM 0
Share

@Zephrim maybe you can help me? i've been trying to change the sprite of a UI image for ages, should be simple like changing a texture... but it isn't, i've used you're answer but i get the following error:

 The type `Image' must be convertible to `UnityEngine.Component' in order to use it as parameter `T' in the generic type or method `UnityEngine.GameObject.GetComponentsInChildren<T>()'

i'm using unity 4.6, in the middle of a project so have not upgraded to 5

thank you I hope you, or someone can help!

avatar image Zephrim · Mar 17, 2015 at 11:18 PM 0
Share

@Carrel Is there any way you could paste the code here so I can have a look at it?

avatar image Thalthanas · Mar 23, 2015 at 09:58 AM 1
Share

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class YourScript: $$anonymous$$onoBehaviour {

 public int x;
 public Sprite red;
 public Sprite green;
 public Sprite blue;


 // Use this for initialization
 void Start () {
         

     x = Random.Range (0, 2);
     if (x == 0)
         gameObject.GetComponent<Image> ().sprite = red;
     if (x == 1)
         gameObject.GetComponent<Image> ().sprite = green;
     if (x == 2)
         gameObject.GetComponent<Image> ().sprite = blue;
 }
 
 // Update is called once per frame
 void Update () {
 
 }

}

with this you can change the sprite w/o changing the whole object. I tried this on Unity5 btw.

Show more comments
avatar image
0

Answer by Thalthanas · Mar 23, 2015 at 09:40 PM

You can also change the sprite w/o changing the whole object like:

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class YourScript: MonoBehaviour {

public Sprite red;

public Sprite green;

public Sprite blue;

void Start () {

     x = Random.Range (0, 2);
     if (x == 0)
         gameObject.GetComponent<Image> ().sprite = red;
     if (x == 1)
         gameObject.GetComponent<Image> ().sprite = green;
     if (x == 2)
         gameObject.GetComponent<Image> ().sprite = blue;
 }

}

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

Answer by epicpython · Nov 24, 2016 at 04:27 AM

Hey, so if gameObject.GetComponent<Image> ().sprite = blue;isn't working for you, try gameObject.GetComponent<SpriteRenderer> ().sprite = blue;

It worked for me, hopefully this helps someone else. :)

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

Answer by aeldron · Nov 23, 2017 at 02:00 PM

For anyone still looking for this answer:

 m_Image = GetComponent<Image>();
 m_Image.sprite = myNewSprite;

https://docs.unity3d.com/ScriptReference/UI.Image-sprite.html

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 4.6 Change the content from a text Object through script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Unity 4.6 UI - set slider value via script 3 Answers

Avoiding click-though in 4.6 1 Answer

Unity 4.6 GUI select button via script 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