i have a simple script for inventory and a script called slot. the slot script has a bool variable called isFull
there are 10 slots in the scene. in the script below the for loop is supposed to loop through all slots until
it find a slot with isFull false and set it to true. but for some reason it set isFull true for all slots.
any help would be appreciated.
thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public slot[] slots;
public void addItem(Item item)
{
for (int i = 0; i < slots.Length; i++)
{
if(slots*.isFull == false)*
{ //able to add item.
// set item in slot. slots*.item = item;* slots*.image.sprite = item.artWork;* slots*.isFull = true;* //disable adding item break; }
I’ll assume that your slots array contains the same slot instance multiple times. So setting one would indeed affect all since there’s only one slot. Using break or return would not make a difference and your code should work as it is, given each array element is actually a seperate slot instance (which I doubt given your problem).
i fixed the issue. the problem was on another script. the issue was really stupid. here is the script its called pickup
using UnityEngine;
public class Pickup : MonoBehaviour
{
public Item item;
public Inventory inventory;
private void Start()
{
}
void OnTriggerEnter (Collider other)
{
if(other.CompareTag("Player"))
{
for (int i = 0; i < inventory.slots.Length; i++)
{
if(!inventory.slots*.isFull)*