Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Changing image on button - image.sprite not working

Discussion in 'Scripting' started by franMx, Mar 21, 2021.

  1. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
    it seems image.sprite is not present in unity 2020. How can I change the actual graphic via c# code?


    Code (CSharp):
    1.    Image t0 = myButtons[0].GetComponent<Image>();
    2.    t0.sprite = theSprite; //<--- doesn't work:'cannot resolve symbol sprite'
    Thanks.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Make sure you have installed and used NOT the UIElements namespace but instead the UnityEngine.UI namespace.

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
    franMx likes this.
  3. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
    Thank you very much, will edit original question for code formating.
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Actually seejay, I don't think he wants that one, but I could be wrong. I think he wants the UnityEngine.UI stuff, not the UIElements stuff, and I base that just on him talking about buttons, which usually means in the UI stuff.
     
  6. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    692
    Oh, that's right, it's the image inside the button. I read it too fast ;)

    This works. I think the OP had an issue with theSprite, not with the implementation. Needs to be a sprite-based texture.


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class buttonTestScript : MonoBehaviour
    5. {
    6.     Image myButtonImage;
    7.     public Sprite myButtonSprite;
    8.  
    9.     void Start()
    10.     {
    11.         myButtonImage = GetComponent<Image>();
    12.         myButtonImage.color = new Color(1, 0, 0, 1); // because you can
    13.         myButtonImage.sprite = myButtonSprite;
    14.     }
    15.  
    16. }
    17.  
     
    Nebuans likes this.
  7. franMx

    franMx

    Joined:
    May 27, 2013
    Posts:
    30
    Hi, I was missing the correct reference indeed, as posted by Kurt-Dekker. Don't see a "solved" button, but it's now.

    Thanks a lot for your help.