Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

Managing multiple images fillamount by one function? 1 Answer

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

Save a Image's state 1 Answer

Button calls itself numerous times 0 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