• 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 MaKayla_Panda · Jun 20, 2019 at 03:08 AM · scripting beginner2d sprites

How to display one image at a time

Hello! I am trying to created a 2D dress up game. I am going to have several different drop down menus where you can select different clothing items. However, I need to display one image at a time per menu. For example, a shirt is selected, but the player wants to select a different shirt from the menu and it changes to the new one while removing the previously selected shirt. Is there a way to do this for a sprite?

Thank you!

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

2 Replies

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

Answer by Freesmith · Jun 20, 2019 at 04:08 AM

Hi, Try attaching this script to a game object with a sprite renderer component, drag several sprites from project to sprite list array in the script inspector, press play and hit space to change sprites.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ChangeSprite : MonoBehaviour
 {
     [SerializeField] private Sprite[] spriteList;
     private SpriteRenderer spriteRend;
     [SerializeField] private int indexR;
     private void Start()
     {
         spriteRend = this.GetComponent<SpriteRenderer>();
     }
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             if (spriteList.Length >0)
             {
                 indexR = Random.Range(0,spriteList.Length);
             }
             ChangeSpite(indexR);
         }
     }
     public void ChangeSpite(int index)
     {
         if (index < spriteList.Length)
         {
             spriteRend.sprite = spriteList[index];
         }
 
     }
 
 
 }
 

If you use a button to triger sprite change then you want to make a new button on click event , drag game object with a script to an object field , select a changesprite method from a changesprite class and set integer paramter. here is the screenshot link text

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 MaKayla_Panda · Jun 21, 2019 at 12:54 AM 0
Share

Thank you for this script! I tested the script and it works as described. This will definitely help me generate different sprites for my game!

avatar image
0

Answer by Divinitize1 · Jun 20, 2019 at 03:35 AM

If i understand correctly. And assuming you're using unity UI.

 //the main image
 Public Image shirt;
 //array of shirt sprites
 Public Sprite[] shirts;
 //set the main image to the first sprite in your array
 shirt.GetComponent<Sprite>.sprite = shirts[0];

I'm on my phone and can't test that but it should be kind of what you're after. I may have the capitalization incorrect for sprite in the component might be .Sprite. I'm no expert myself but until someone more experienced posts an answer you can try your luck with that.

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 MaKayla_Panda · Jun 20, 2019 at 04:06 AM 0
Share

Thank you! I'll try this script out. Do i apply this script to the game object its self or the button I'm using to select/deselect the objects?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

117 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

Related Questions

Change texture by using the name of the previous texture? 1 Answer

How to make a collision script 1 Answer

GetAxis to GetTouch 0 Answers

How can I populate a list of GameObjects from the prefabs folder? 1 Answer

Augmented reality - LeanTouch/Lean Touch - What to do with multiple objects & how to reset position 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