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 /
avatar image
0
Question by Neon_Apollo · Oct 30, 2019 at 08:56 AM · unity 2dif-statementsspriterenderertags

Multiple If Statements causing problems with changing sprites

Hey so I'm having problems getting different sprites to show up depending on a gameobjects tag. The problem seems to come from having multiple if statements checking the objects tag for all the different sprites, here's an excerpt from the script that worked fine:

     public GameObject Canada;
     public Sprite CanadaUN;
     
        void Start()
         {
             Canada.tag = "UNZone";
         }
     
         void Update()
         {
             if (Canada.tag == "UNZone")
             {
                 Canada.GetComponent<SpriteRenderer>().sprite = CanadaUN;
             }
         }

But it seems when I add more sprites and make more if statements like above to have them show up if the tag changes it stops working. Any ideas?

Comment
Add comment · Show 4
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 cdr9042 · Oct 30, 2019 at 09:14 AM 1
Share

First, don't compare string in Update() like that, it's expensive. Do the tag change mid-play in-game and you need to check its tag every Update() like that?

Secondly, post the code that you added more if statements, I don't see anything wrong here yet

avatar image Neon_Apollo cdr9042 · Oct 30, 2019 at 09:48 AM 0
Share

I'm not sure what you mean in the first part, I'm still pretty new to all this. Here's the rest:

  public GameObject Canada;
  public Sprite CanadaUN;
  public Sprite CanadaAL;
  public Sprite CanadaDO;
  public Sprite CanadaGI;
  public Sprite CanadaVA;
  
     void Start()
      {
          Canada.tag = "UNZone";
      }
  
      void Update()
      {
          if (Canada.tag == "UNZone")
          {
              Canada.GetComponent<SpriteRenderer>().sprite = CanadaUN;
          }
          if (Canada.tag == "ALZone")
          {
              Canada.GetComponent<SpriteRenderer>().sprite = CanadaAL;
          }
          if (Canada.tag == "DOZone")
          {
              Canada.GetComponent<SpriteRenderer>().sprite = CanadaDO;
          }
          if (Canada.tag == "GIZone")
          {
              Canada.GetComponent<SpriteRenderer>().sprite = CanadaGI;
          }
          if (Canada.tag == "VAZone")
          {
              Canada.GetComponent<SpriteRenderer>().sprite = CanadaVA;
          }
      }

If you mean doing the tag checks like that is expensive that is probably the problem because I have a bunch more for all the different gameobjects that I need to change sprites for in the same way, never seen it done any other way though.

avatar image cdr9042 Neon_Apollo · Oct 30, 2019 at 10:08 AM 0
Share

how did you set its tag? Do you set it in the editor's inspector?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by revolute · Oct 30, 2019 at 10:57 AM

 [System.Serializable]
 public class Tag{
     public string tag;
     public Sprite sprite;
 }
 public List<Tag> tagList = new List<Tag>();

will let you work with the tags and sprite matching in the inspector.

Then, loop through the list to check if any matches.

 foreach(var i in tagList){
     if(Canada.tag == i.tag)
         Canada.GetComponent<SpriteRenderer>().sprite = i.sprite;
 }
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 Neon_Apollo · Oct 30, 2019 at 11:40 AM 0
Share

Thanks for the reply, hope it can help someone in the future. Turns out my problem was that I hadnt assigned all the gameobjects in the inspector, I have quite a few on that script so I didn't want to assign all of them if it wouldn't work, turned out that was the issue

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

120 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

Related Questions

SpriteRenderer becomes invisible when rotated using animation and built for android unity 5.3.1 1 Answer

Match 3 game getting the colour of the match 1 Answer

Unity2D Sprite Fading Issues 1 Answer

How to change a 2d sprite to another sprite in an array? 0 Answers

Why wont my sprite renderer show when enabled? 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