- Home /
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()
{
}
}
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++;
}
}
Ill try that, it may be useful, especially when i add more items
Wow, this worked perfectly! cant believe i didnt think of it!
I cannot thank you enough!!!
Your answer
Follow this Question
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