Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 NaveGCT · Aug 11, 2020 at 08:50 PM · uiimageif-statementsinventory system

Problem with if statements relating to images

So, i need a simple 3 slot inventory system in my game- i tried some of the more efficient methods, but they didnt work, so i opted to use this less efficient, but more reliable for my needs, system (so far there are just two items, called "sun" and "flashlight" respectively. This is my script; I have some playerpref settings as to which are equipped currently. The way it works is; first it checks if the flashlight is equipped; if it is, then the flashlight image (assets.flashlightSprite) goes into the first availible slot.
Then it does the same with the sun item. But the problem is, all the items go to the first slot. If i have flashlight enabled and nothing else, it goes to the first slot. if i have both flashlight and sun enabled, then the sun is in the first slot but the flashlight isnt even there (what is suppost to happen is flashlight in first slot, sun in second slot). if no items are there it works fine. What is the problem with this code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class MenuTypes : MonoBehaviour
 {
     public bool SlotOneEmpty = true;
     public bool SlotTwoEmpty = true;
     public bool SlotThreeEmpty = true;
     public Image Slot1;
     public Image Slot2;
     public Image Slot3;
     public int FlashID = 17;
     public int SunID = 17;
     public ItemAssets assets;
 
     // Start is called before the first frame update
     void Start()
     {
         EquipStuff();
     }
 
     void EquipStuff()
     {
         if (PlayerPrefs.GetInt("flashequipped") == 1)
         {
             if (SlotOneEmpty == true)
             {
                 FlashID = 1;
                 Slot1.sprite = assets.flashlightSprite;
                 SlotOneEmpty = false;
             } else
             {
                 if (SlotTwoEmpty == true)
                 {
                     FlashID = 2;
                     Slot2.sprite = assets.flashlightSprite;
                     SlotTwoEmpty = false;
                 } else
                 {
                     if (SlotThreeEmpty == true)
                     {
                         FlashID = 3;
                         Slot3.sprite = assets.flashlightSprite;
                         SlotThreeEmpty = false;
                     } else
                     {
                         Debug.Log("No space for flashlight!");
                     }
                 }
             }
         }
         if (PlayerPrefs.GetInt("sunequipped") == 1)
         {
             if (SlotOneEmpty == true)
             {
                 SunID = 1;
                 Slot1.sprite = assets.personalsunSprite;
                 SlotOneEmpty = false;
             }
             else
             {
                 if (SlotTwoEmpty == true)
                 {
                     SunID = 2;
                     Slot1.sprite = assets.personalsunSprite;
                     SlotTwoEmpty = false;
                 }
                 else
                 {
                     if (SlotThreeEmpty == true)
                     {
                         SunID = 3;
                         Slot3.sprite = assets.personalsunSprite;
                         SlotThreeEmpty = false;
                     }
                     else
                     {
                         Debug.Log("No space for flashlight!");
                     }
                 }
             }
         }
     }
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 
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 FlaSh-G · Aug 11, 2020 at 08:58 PM

What about this?

 public Image[] slots;

.

 private void EquipStuff()
 {
   var smallestEmptySlotIndex = 0;
 
   if (PlayerPrefs.GetInt("flashequipped") == 1)
   {
     slots[smallestEmptySlotIndex] = assets.flashlightSprite;
     smallestEmptySlotIndex++;
   }
 
   if (PlayerPrefs.GetInt("sunequipped") == 1)
   {
     slots[smallestEmptySlotIndex] = assets.personalsunSprite;
     smallestEmptySlotIndex++;
   }
 }
Comment
Add comment · Show 2 · 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 NaveGCT · Aug 11, 2020 at 10:43 PM 0
Share

Ill try that, it may be useful, especially when i add more items

avatar image NaveGCT · Aug 11, 2020 at 10:49 PM 0
Share

Wow, this worked perfectly! cant believe i didnt think of it!

I cannot thank you enough!!!

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

227 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Drag handler is blocking image IPointerClick but not button OnClick 0 Answers

LeanTween.moveLocal sometimes snaps to position rather than moving over time 0 Answers

Vertical Layout draw order, child appears above parent 2 Answers

UI resizing in game 1 Answer

Buttons become invisible when changing color (on Button or on Image) in script -1 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