• 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 /
  • Help Room /
avatar image
0
Question by Lineweaver · Apr 11, 2019 at 08:18 PM · buttonscript.imagespritesui image

Change sprite of a UI image throug button click (c#)

I have four different images with a button component, and a script that looks like this:

 public class GodSelect : MonoBehaviour {

 GameObject S1;
 GameObject S2;

 // Use this for initialization
 void Start ()
 {
     S1 = GameObject.Find("Skill 1");
     S2 = GameObject.Find("Skill 2");
 }

 public void Thor()
 {
     Debug.Log("Thor selected");
 }

 public void Tyr()
 {
     Debug.Log("Tyr selected");
 }

 public void Skadi()
 {
     Debug.Log("Skadi selected");
 }

 public void Hel()
 {
     Debug.Log("Hel selected");
 }
 }

The debug part works. S1 and S2 are images. What I want to do is change the functions so whenever one function is activated, the sprites of S1 and S2 change to different sprites. For example, activating the Thor function will change the sprites of S1 to one sprite, and S2 to a different sprite. the Tyr function will do the same with two more different sprites, and so on.

I tried searching for an answer but I keep getting stuck and I don't know what to do anymore. If anyone can figure this out and help me, I'll be really grateful ^^

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

1 Reply

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

Answer by Hellium · Apr 11, 2019 at 08:50 PM

 public struct God
 {
     public string Name;
     public Sprite S1;
     public Sprite S2;
 }
 
 public class GodSelect : MonoBehaviour
 {
      // Drag & drop the gameObject holding the image component in the inspector
      public Image GodImage1;
 
      // Drag & drop the gameObject holding the image component in the inspector
      public Image GodImage2;
 
      // Fill the array in the inspector
      public God[] Gods;

      // Specify this function in the `onClick` event of your buttons
      // And give the name of the god you have set in the inspector
      public void SelectGod( string name )
      {
          for( int godIndex = 0 ; godIndex < Gods.Length ; ++godIndex )
          {
               if( string.Equals( Gods[godIndex].Name, name, System.StringComparison.InvariantCultureIgnoreCase ) )
               {
                    Debug.Log( Gods[godIndex].Name + " selected" ) ;
                    GodImage1.sprite = Gods[godIndex].S1;
                    GodImage2.sprite = Gods[godIndex].S2;
                    break;
               }
          }
      }
  }



You could do something better (IMO) by making God inherit from ScriptableObject, and defining SelectGod as follow:

      // Specify this function in the `onClick` event of your buttons
      // And drag & drop the scriptable object in the inspector
      public void SelectGod( God god )
      {
           Debug.Log( god.Name + " selected" ) ;
           GodImage1.sprite = god.S1;
           GodImage2.sprite = god.S2;
      }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

197 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 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 get Access of image as sprit in scripts 0 Answers

Keep button's image size same as original sprite 1 Answer

Managing multiple images fillamount by one function? 1 Answer

Make only visible portion of image clickable 0 Answers

every time i import an image(sprites) and edit it on sprite editor i always get this error message, i've already tried looking for some answers but it seems i need to post it here. . and i do hope anyone could help me, my unity version is 4.7.1 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges