- Home /
Question by
$$anonymous$$ · Oct 23, 2017 at 09:43 AM ·
uiui imageinventory systemmousewheelslot
Problem when scrolling through inventory slots
Hi, I'm almost finished making my inventory system, and I am using the mouse wheel as a method for the player to scroll through their inventory.slots. The problem here is that the GUI that hovers over the in-game selected slots is out of sync with the actual selected slot, it will always be one slot ahead for some reason. The other problem is that when the scrolling forwards through the slots, if scrolled backwards once, it will still go forwards another slot, and if scrolled backwards again, it will go back a slot, and the same for vise versa. I have no clue why it is doing this, but it is a strange issue and is really bumming me out. :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InventoryScript : MonoBehaviour {
public PropertiesScript VarPropertiesScript;
public StatsManagerScript VarStatsManagerScript;
public GameObject InventoryUI;
public GameObject PutInInv;
public GameObject Player;
public GameObject InventorySlot1;
public GameObject InventorySlot2;
public GameObject InventorySlot3;
public GameObject InventorySlot4;
public GameObject InventorySlot5;
public GameObject InventorySlot6;
public GameObject InventorySlot7;
public GameObject InventorySlot8;
public GameObject InventorySlot9;
private GameObject InventorySlotItem1;
private GameObject InventorySlotItem2;
private GameObject InventorySlotItem3;
private GameObject InventorySlotItem4;
private GameObject InventorySlotItem5;
private GameObject InventorySlotItem6;
private GameObject InventorySlotItem7;
private GameObject InventorySlotItem8;
private GameObject InventorySlotItem9;
public string ButtonToOpenInventory;
static public GameObject LookingAt;
public GameObject GUISlots;
public GameObject SelectedSlot;
public string ButtonToPickup;
public Text InventorySlotItemText1;
public Text InventorySlotItemText2;
public Text InventorySlotItemText3;
public Text InventorySlotItemText4;
public Text InventorySlotItemText5;
public Text InventorySlotItemText6;
public Text InventorySlotItemText7;
public Text InventorySlotItemText8;
public Text InventorySlotItemText9;
public string NullText;
private int SelectedSlotNumber = 1;
private int SelectDisappearTimer;
void Start () {
}
void Update () {
if (SelectedSlotNumber > 9) {
SelectedSlotNumber = 1;
}
if (SelectedSlotNumber < 1) {
SelectedSlotNumber = 9;
}
SelectDisappearTimer += 1;
SelectedSlot = GameObject.Find ("Slot" + SelectedSlotNumber);
if (InventorySlotItem1 == null && PutInInv != null) {
InventorySlotItem1 = PutInInv;
InventorySlotItemText1.text = InventorySlotItem1.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem2 == null && PutInInv != null) {
InventorySlotItem2 = PutInInv;
InventorySlotItemText2.text = InventorySlotItem2.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem3 == null && PutInInv != null) {
InventorySlotItem3 = PutInInv;
InventorySlotItemText3.text = InventorySlotItem3.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem4 == null && PutInInv != null) {
InventorySlotItem4 = PutInInv;
InventorySlotItemText4.text = InventorySlotItem4.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem5 == null && PutInInv != null) {
InventorySlotItem5 = PutInInv;
InventorySlotItemText5.text = InventorySlotItem5.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem6 == null && PutInInv != null) {
InventorySlotItem6 = PutInInv;
InventorySlotItemText6.text = InventorySlotItem6.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem7 == null && PutInInv != null) {
InventorySlotItem7 = PutInInv;
InventorySlotItemText7.text = InventorySlotItem7.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem8 == null && PutInInv != null) {
InventorySlotItem8 = PutInInv;
InventorySlotItemText8.text = InventorySlotItem8.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (InventorySlotItem9 == null && PutInInv != null) {
InventorySlotItem9 = PutInInv;
InventorySlotItemText9.text = InventorySlotItem9.name;
PutInInv.gameObject.SetActive(false);
PutInInv = null;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0) {
SelectDisappearTimer = 0;
SelectedSlotNumber += 1;
GUISlots.SetActive (true);
GUISlots.transform.position = new Vector3 (SelectedSlot.transform.position.x, SelectedSlot.transform.position.y + 32, 0);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) {
SelectDisappearTimer = 0;
SelectedSlotNumber += -1;
GUISlots.SetActive (true);
GUISlots.transform.position = new Vector3 (SelectedSlot.transform.position.x, SelectedSlot.transform.position.y + 32, 0);
}
if (SelectedSlot != null && SelectDisappearTimer >= 300){
SelectedSlot = null;
GUISlots.SetActive (false);
}
if (Input.GetKeyDown (ButtonToPickup)){
LookingAt = VarStatsManagerScript.LookingAt;
VarPropertiesScript = LookingAt.GetComponent<PropertiesScript> ();
if (VarPropertiesScript.Pickupable == true && PutInInv == null && LookingAt != null){
PutInInv = VarStatsManagerScript.LookingAt;
}
if (Input.GetKeyDown (ButtonToOpenInventory)){
if (InventoryUI.activeSelf == false) {
InventoryUI.SetActive (true);
} else {
InventoryUI.SetActive (false);
}
}
}
}
}
Comment