#Hotbar Help
1 messages · Page 1 of 1 (latest)
public class Inventory {
Hotbar[] Hotbars = new[5];
}
public class Hotbar {
Slot[] Slots = new[6];
}
ok
so i have 6 slots
and 5 hotbars
and id use those
wait
why they in there own class
You dont understand arrays as you said you do
So they can have methods on them to run self-contained logic
Your C# version is probably too old to use the new[x] you need to use new Hotbar[x] etc.
Whats the problem with making new classes?
You're just using GameObject, which has no useful functionality related to the inventory slots, the hotbar, etc. I imagine your code is all this monolithic stuff in Inventory
whereas I would put the code that is about the hotbar in the hotbar class. The code that sets up the slot in the slot class
right
when you create a new hotbar to use it, you don't have to change any code, you just access the new index of your hotbar array and call the method. The code looks exactly the same
public class Hotbar
{
Hotbar[] Hotbars = new Hotbar[5];
}
public class Slot
{
Slot[] Slots = new Slot[6];
}```
so i have this rn
so declaring hotbar as 5 means it has a size of 5 correct?
Sure, 5 empty elements. It won't do anything useful, it was an example of structure vs your numbered lists
but i have 5 hotbars so i should declare it as 4 because i will use 0
same with slots i should make that 5
No
ok
so now my question is how do i access these from outside the skript, i want to be able to drag in say a prefab to slot 2 of hotbar 3
is that possible with this setup
The hotbar could be serializable, and the slots could be monobehaviours on your slot prefabs that are dragged into that array in the inventory
your above code is also wrong, the hotbar shouldn't have a hotbars array, the inventory has that.
I do not have the time to lead you through the setup and coding of this beyond what I've mentioned. If you don't get it, I'd only recommend watching some tutorials, or just trying to make it work with your original setup
oh ok ig u just give up rip, time to watch tutorials
have u atleast got a good tutorial to link me?
because the tutorial im watching uses lists just like me
So you said you can't access the slots array. That's because it is private
so if i make the class public ill see it?
For unity to save or show your custom classes (Hotbar, Slots) they need the [System.Serializable] attribute
so i add [SerializeField] at the start?
[System.Serializable]
class Hotbar``` etc
And with etc I mean add it to the Slot class also
Wait not SerializeField. It is System.Serializable
Like I said
ok
still the same
[System.Serializable]
class Hotbar
{
public Slot[] slots = new Slot[6];
}
[System.Serializable]
class Slot
{
}```
if i type "slots" it says it dont exist
thats outside of the hotbar class
Oh that is the issue
You need to get a reference to an instance of a Hotbar
Like lets say you want the first:cs Hotbar firstHotbar = Hotbars[0];
Then you access its slots withcs firstHotbar.slots. //...
ok
Is your Hotbars array serialized? In the code you posted, its not
It needs to be public or [SerializeField]
[SerializeField] Hotbar[] Hotbars = new Hotbar[6];
[System.Serializable]
class Hotbar
{
public Slot[] slots = new Slot[6];
}
[System.Serializable]
class Slot
{
}``` like this?
Yeah, but now that I look at it, that just makes an array of 6 null objects I think
Change this: cs [SerializeField] Hotbar[] Hotbars = new Hotbar[6];
To a list:cs [SerializeField] List<Hotbar> Hotbars = new List<Hotbar>();
alr done
Can you see the Hotbars list in the inspector of Inventory?
Nice, just to make sure, add a public variable to Slot class and see if you can see it in that list?
[System.Serializable]
class Slot
{
public int testInt = 123;
}```
Ok yeah just making sure
Remove that and now you can proceed with whatever you wanted to make
so if i just changed that to a gameobject i would be able to change every slot of every hotbar
danm this is way better
thanks
Yeah