- Home /
The question is answered, right answer was accepted
for loop not breaking with if statement
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[i].isFull == false)
{
//able to add item.
// set item in slot.
slots[i].item = item;
slots[i].image.sprite = item.artWork;
slots[i].isFull = true;
//disable adding item
break;
}
}
}
}
The code looks fine to me.
I'd guess slots is empty or slots[i].isFull is already set to true on all items?
i looked at it but thats not the case. isFull is false and slots array is full.
Answer by RayyFarr · Nov 10, 2021 at 11:26 AM
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[i].isFull)
{
inventory.addItem(item);
Debug.Log("Adding item");
Destroy(gameObject);
}
}
}
}
}
after fix
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public Item item;
public Inventory inventory;
private void Start()
{
}
void OnTriggerEnter(Collider other)
{
inventory.addItem(item);
Debug.Log("Adding item");
Destroy(gameObject);
}
}
fore some reason i didnt check this script.
Answer by evskii · Nov 10, 2021 at 08:39 AM
Try using return; rather than break; and see what happens.
return; exists the whole method whereas break; only exists the current switch/loop so using return; might help.
Otherwise you can add an extra check to see if an item has been added that method call. E.G:
public void addItem(Item item)
{
bool hasBeenAdded = false; //HERE
for (int i = 0; i < slots.Length; i++)
{
if(slots[i].isFull == false && hasBeenAdded == false)
{
//able to add item.
// set item in slot.
slots[i].item = item;
slots[i].image.sprite = item.artWork;
slots[i].isFull = true;
hasBeenAdded = true; //HERE
//disable adding item
break;
}
}
}
Answer by Bunny83 · Nov 10, 2021 at 09:12 AM
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).
ps: class or generally type names should start with a capital letter.
Follow this Question
Related Questions
More than one row in an array not displaying in a FOR loop 2 Answers
using "for" in Unity Script HELP! 2 Answers
Difference between if statement, while and for loop? 4 Answers
Why is my FOR loop Accelerating each time it is used? 2 Answers
For loop multiplying the action by the lenght of the array.. WHY ?! 1 Answer