This For Loop is killing me

Can someone shed a little light on why the following code isn’t working the way I want it to? I’ve written and searched, and now i’m here, admitting defeat.

Obviously it’s part of a bigger document, but I’ve pinpointed the problem to the For Loop. I can post the rest of the code on request.

All i’m looking to do search through the inventory and check if the sprite already exists and if it does - increase it by 1. The rest of the function works, but the “var item” only holds on to the first inventory child (which is an item slot). It does increase that slot number by the correct amount of items i’ve grabbed.

So I want the loop to check if i already have the object in my inventory by matching anything (name, sprite, w/e…i’ve chosen name at the moment) and then increase it by one.

else if (slotCount > 0 && !isSprite) {

for (var item : Transform in inventory.transform) {

if (item.gameObject.name == col.gameObject.name) {

if (!isSprite && (tag == “Consumable” || tag == “Resource” )) {
var c : String = item.transform.Find(“Text”).GetComponent(Text).text;
var tCount : int = System.Int32.Parse(c) + 1;
item.transform.Find(“Text”).GetComponent(Text).text = “” + tCount;
Destroy(col.gameObject, 0.4);
isSprite = true;
return;
}
}

Help me and forever be my master.

You are only going in the “if” block inside the loop if “isSprite” is false. But you set it to true after the first item - hence it will not go into the if a second time

It makes sense, but I make it false later in the script. Regardless I tried it, but it didn’t change anything.

#pragma strict

import UnityEngine.UI;
import System.Collections.Generic;

var text : Text;
var anim: Animator;
public var isSprite : boolean;
public var isMoney : boolean;
public var isPickUp : boolean;

public var slotPrefab : GameObject;
var inventory : GameObject;
var itemSprite : GameObject;

var slotCount : int;
var currentAmount : int = 0;

var spriteImage;
var spriteTag;
var spriteName;

function Awake () {
inventory = GameObject.FindGameObjectWithTag(“Inventory”);
var moneyText = GameObject.FindGameObjectWithTag(“Bank”).GetComponent(Text);
}

function Update () {
slotCount = inventory.transform.childCount;
}

function OnTriggerStay(col : Collider) {
if(Input.GetKeyDown(KeyCode.F) && !isPickUp) {
var tag = col.gameObject.tag;
anim.SetTrigger(“PickUp”);

if(tag == “Money” && !isMoney) {
PickUpMoney();
Destroy(col.gameObject,.4);
isMoney = true;
}

if (slotCount < 15 && (tag == “Weapon” || tag == “Torso” ||tag == “Consumable” || tag == “Resource”)) {
spriteImage = col.gameObject.GetComponent(Image).sprite;
spriteTag = col.gameObject.tag;
spriteName = col.gameObject.name;

if (slotCount == 0 && !isSprite) {
ItemToInventory();
Destroy(col.gameObject, 0.4);
isSprite = true;
}

else if (slotCount > 0 && !isSprite) {

for (var item : Transform in inventory.transform) {

if (col.gameObject.GetComponent(Image).sprite == item.gameObject.GetComponent(Image).sprite) {

if (/*!isSprite && */ (tag == “Consumable” || tag == “Resource” )) {
var c : String = item.transform.Find(“Text”).GetComponent(Text).text;
var tCount : int = System.Int32.Parse(c) + 1;
item.transform.Find(“Text”).GetComponent(Text).text = “” + tCount;
Destroy(col.gameObject, 0.4);
//isSprite = true;
return;
}
}

else {
if (!isSprite) {
print(“instantiate”);
ItemToInventory();
Destroy(col.gameObject, 0.4);
isSprite = true;
}
}
break;
}

isSprite = true;
}

}
}

else {
anim.SetBool(“isIdle”, true);
isSprite = false;
isPickUp = false;
isMoney = false;
}

}

function PickUpMoney() {
var moneyText = GameObject.FindGameObjectWithTag(“Bank”).GetComponent(Text);
var moneyAmount : int = Random.Range(5,10);
currentAmount += moneyAmount;
moneyText.text = "₳ " + currentAmount;
}

function ItemToInventory() {
itemSprite = Instantiate(slotPrefab) as GameObject;
itemSprite.transform.SetParent(inventory.transform);
itemSprite.AddComponent(Image).sprite = spriteImage;
itemSprite.tag = spriteTag;
itemSprite.name = spriteName;
}

You are breaking (“break;”) out of the for loop after the first item.
Also: you “return” after the first item was found - but I think thats intended, but would also result in only 1 item being progressed if the first one would be a hit.

Also - Please use Code-Tags. Using code tags properly - Unity Engine - Unity Discussions

This is much more readable:

#pragma strict
import UnityEngine.UI;
import System.Collections.Generic;
var text : Text;
var anim: Animator;
public var isSprite : boolean;
public var isMoney : boolean;
public var isPickUp : boolean;
public var slotPrefab : GameObject;
var inventory : GameObject;
var itemSprite : GameObject;
var slotCount : int;
var currentAmount : int = 0;
var spriteImage;
var spriteTag;
var spriteName;
function Awake () {
    inventory = GameObject.FindGameObjectWithTag("Inventory");
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
}
function Update () {
    slotCount = inventory.transform.childCount;
}
function OnTriggerStay(col : Collider) {
    if(Input.GetKeyDown(KeyCode.F) && !isPickUp) {
        var tag = col.gameObject.tag;
        anim.SetTrigger("PickUp");
    
        if(tag == "Money" && !isMoney) {
            PickUpMoney();
            Destroy(col.gameObject,.4);
            isMoney = true;
        }
 
        if (slotCount < 15 && (tag == "Weapon" || tag == "Torso" ||tag == "Consumable" || tag == "Resource")) {
            spriteImage = col.gameObject.GetComponent(Image).sprite;
            spriteTag = col.gameObject.tag;
            spriteName = col.gameObject.name;
        
            if (slotCount == 0 && !isSprite) {
                ItemToInventory();
                Destroy(col.gameObject, 0.4);
                isSprite = true;
            }
    
            else if (slotCount > 0 && !isSprite) {
        
                for (var item : Transform in inventory.transform) {
            
                    if (col.gameObject.GetComponent(Image).sprite == item.gameObject.GetComponent(Image).sprite) {
                
                        if (/*!isSprite && */ (tag == "Consumable" || tag == "Resource" )) {
                            var c : String = item.transform.Find("Text").GetComponent(Text).text;
                            var tCount : int = System.Int32.Parse(c) + 1;
                            item.transform.Find("Text").GetComponent(Text).text = "" + tCount;
                            Destroy(col.gameObject, 0.4);
                            //isSprite = true;
                            return;
                        }
                    }
            
                    else {
                        if (!isSprite) {
                            print("instantiate");
                            ItemToInventory();
                            Destroy(col.gameObject, 0.4);
                            isSprite = true;
                        }
                    }
                    break;
                }        
                isSprite = true;
            }
        }
    }
    else {
        anim.SetBool("isIdle", true);
        isSprite = false;
        isPickUp = false;
        isMoney = false;
    }
}
function PickUpMoney() {
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
    var moneyAmount : int = Random.Range(5,10);
    currentAmount += moneyAmount;
    moneyText.text = "₳ " + currentAmount;
}
function ItemToInventory() {
    itemSprite = Instantiate(slotPrefab) as GameObject;
    itemSprite.transform.SetParent(inventory.transform);
    itemSprite.AddComponent(Image).sprite = spriteImage;
    itemSprite.tag = spriteTag;
    itemSprite.name = spriteName;
}

I deleted both “return” and “break” but it’s still not adding up other items after the first one.

Thanks for the link. I wasn’t aware of the code tags in the thread, but now I am.

Okay, so I removed the boolean isSprite from the ’ if (tag == “Consumable” || tag == “Resource” ) 'and you were right, that’s definitely affecting it. Now it’s adding up other items, but also instantiating that item.

The “return” part was just a “maybe” - I think it was more the “issprite” and/or the break. maybe thats the reason for it now also instantiating?

I’ve taken away some other booleans but it doesn’t seem to change anything. I think it has to do with the fact that the else statement is within the loop, but i’m not 100% sure. It counts up by the amount the first item of that kind has. So apple has 2, then grab another apple and it has 3, with 3 more instants of apples.

Can you please show your code again?

#pragma strict

import UnityEngine.UI;
import System.Collections.Generic;

var text : Text;
var anim: Animator;
public var isSprite : boolean;
public var isMoney : boolean;
public var isPickUp : boolean;

public var slotPrefab : GameObject;
var inventory : GameObject;
var itemSprite : GameObject;

var slotCount : int;
var currentAmount : int = 0;

var spriteImage;
var spriteTag;
var spriteName;
var item;

function Awake () {
        inventory = GameObject.FindGameObjectWithTag("Inventory");
        var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
}

function Update () {
        slotCount = inventory.transform.childCount;
}

function OnTriggerStay(col : Collider) {
    if(Input.GetKeyDown(KeyCode.F) && !isPickUp) {
    var tag = col.gameObject.tag;
    anim.SetTrigger("PickUp");
      
        if(tag == "Money" && !isMoney) {
               PickUpMoney();
            Destroy(col.gameObject,.4);
            isMoney = true;
        }

        if (slotCount < 15 && (tag == "Weapon" || tag == "Torso" ||tag == "Consumable" || tag == "Resource")) {
            spriteImage = col.gameObject.GetComponent(Image).sprite;
            spriteTag = col.gameObject.tag;
            spriteName = col.gameObject.name;

            if (slotCount == 0 && !isSprite) {
                ItemToInventory();
                Destroy(col.gameObject, 0.4);
                isSprite = true;
                } 

            else if (slotCount > 0 /*&& !isSprite*/) {
                   
                    for (var item : Transform in inventory.transform) {

                    if (col.gameObject.GetComponent(Image).sprite == item.gameObject.GetComponent(Image).sprite) {

                        if (tag == "Consumable" || tag == "Resource" ) {
                        var c : String = item.transform.Find("Text").GetComponent(Text).text;
                        var tCount : int = System.Int32.Parse(c) + 1;
                        item.transform.Find("Text").GetComponent(Text).text = "" + tCount;
                        Destroy(col.gameObject, 0.4);
                        }
                    }

                    else {

                        if (col.gameObject.GetComponent(Image).sprite != item.gameObject.GetComponent(Image).sprite) {
                        ItemToInventory();
                        Destroy(col.gameObject, 0.4);
                        //isSprite = true;

                    }
                    }
               
                   }

                   //isSprite = true;
                   }

            }
        }

     else {
        anim.SetBool("isIdle", true);
        isSprite = false;
           isPickUp = false;
           isMoney = false;
         }      
   
}


function PickUpMoney() {
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
    var moneyAmount : int = Random.Range(5,10);
       currentAmount += moneyAmount;
    moneyText.text = "₳ " + currentAmount;
}


function ItemToInventory() {
itemSprite = Instantiate(slotPrefab) as GameObject;
itemSprite.transform.SetParent(inventory.transform);
itemSprite.AddComponent(Image).sprite = spriteImage;
itemSprite.tag = spriteTag;
itemSprite.name = spriteName;
}

Please add “return;” before line 66 again and try again.

no change

Okay, it’s almost working. When it counts up, it only instantiates a single one. This is my current code.

#pragma strict

import UnityEngine.UI;
import System.Collections.Generic;

var text : Text;
var anim: Animator;
public var isSprite : boolean;
public var isMoney : boolean;
public var isPickUp : boolean;

public var slotPrefab : GameObject;
var inventory : GameObject;
var itemSprite : GameObject;

var slotCount : int;
var currentAmount : int = 0;

var spriteImage;
var spriteTag;
var spriteName;
var item;

function Awake () {
        inventory = GameObject.FindGameObjectWithTag("Inventory");
        var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
}

function Update () {
        slotCount = inventory.transform.childCount;
}

function OnTriggerStay(col : Collider) {
    if(Input.GetKeyDown(KeyCode.F) && !isPickUp) {
    var tag = col.gameObject.tag;
    anim.SetTrigger("PickUp");
      
        if(tag == "Money" && !isMoney) {
               PickUpMoney();
            Destroy(col.gameObject,.4);
            isMoney = true;
        }

        if (slotCount < 15 && (tag == "Weapon" || tag == "Torso" ||tag == "Consumable" || tag == "Resource")) {
            spriteImage = col.gameObject.GetComponent(Image).sprite;
            spriteTag = col.gameObject.tag;
            spriteName = col.gameObject.name;

            if (slotCount == 0 && !isSprite) {
                ItemToInventory();
                Destroy(col.gameObject, 0.4);
                isSprite = true;
                } 

            else if (slotCount > 0 && !isSprite) {
                   
                    for (var item : Transform in inventory.transform) {

                    if (col.gameObject.GetComponent(Image).sprite == item.gameObject.GetComponent(Image).sprite) {

                        if (tag == "Consumable" || tag == "Resource" ) {
                        var c : String = item.Find("Text").GetComponent(Text).text;
                        var tCount : int = System.Int32.Parse(c) + 1;
                        item.Find("Text").GetComponent(Text).text = "" + tCount;
                        Destroy(col.gameObject, 0.4);
                        return;
                        }

                    }

                    else {
                        if (!isSprite){
                        ItemToInventory();
                        Destroy(col.gameObject, 0.4);
                        isSprite = true;
                        }
                    }
            
                   }

                   isSprite = true;
                   }

            }
        }

     else {
        anim.SetBool("isIdle", true);
        isSprite = false;
           isPickUp = false;
           isMoney = false;
         }      
   
}


function PickUpMoney() {
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
    var moneyAmount : int = Random.Range(5,10);
       currentAmount += moneyAmount;
    moneyText.text = "₳ " + currentAmount;
}


function ItemToInventory() {
itemSprite = Instantiate(slotPrefab) as GameObject;
itemSprite.transform.SetParent(inventory.transform);
itemSprite.AddComponent(Image).sprite = spriteImage;
itemSprite.tag = spriteTag;
itemSprite.name = spriteName;
}

I still can’t seem to get it to work. The else statement within the loop goes back to the beginning of the loop once done instantiating. I can’t return it either because then it won’t instantiate at all. I’ve tried a million (more like 50) other combinations with booleans and returns, but no luck.

If I can achieve the same by other means, please do tell.

Not sure what this issue has to do with editor or general support. You would have had more help in the right category.

Hey,

please try the following code.

#pragma strict
import UnityEngine.UI;
import System.Collections.Generic;
var text : Text;
var anim: Animator;
public var isSprite : boolean;
public var isMoney : boolean;
public var isPickUp : boolean;
public var slotPrefab : GameObject;

private var needsInstantiation : boolean;
var inventory : GameObject;
var itemSprite : GameObject;
var slotCount : int;
var currentAmount : int = 0;
var spriteImage;
var spriteTag;
var spriteName;
var item;
function Awake () {
    inventory = GameObject.FindGameObjectWithTag("Inventory");
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
}
function Update () {
    slotCount = inventory.transform.childCount;
}
function OnTriggerStay(col : Collider) {
    if(Input.GetKeyDown(KeyCode.F) && !isPickUp) {
        var tag = col.gameObject.tag;
        anim.SetTrigger("PickUp");
        
        if(tag == "Money" && !isMoney) {
            PickUpMoney();
            Destroy(col.gameObject,.4);
            isMoney = true;
        }
    
        if (slotCount < 15 && (tag == "Weapon" || tag == "Torso" ||tag == "Consumable" || tag == "Resource")) {
            spriteImage = col.gameObject.GetComponent(Image).sprite;
            spriteTag = col.gameObject.tag;
            spriteName = col.gameObject.name;
            if (slotCount == 0 && !isSprite) {
                ItemToInventory();
                Destroy(col.gameObject, 0.4);
                isSprite = true;
            }
            else if (slotCount > 0 && !isSprite) {   
                needsInstantiation = true;
                for (var item : Transform in inventory.transform) {
                    if (col.gameObject.GetComponent(Image).sprite == item.gameObject.GetComponent(Image).sprite) {
                        if (tag == "Consumable" || tag == "Resource" ) {
                            var c : String = item.Find("Text").GetComponent(Text).text;
                            var tCount : int = System.Int32.Parse(c) + 1;
                            item.Find("Text").GetComponent(Text).text = "" + tCount;
                            Destroy(col.gameObject, 0.4);
                            needsInstantiation = false;
                            return;
                        }
                    }                   
                }
                if (needsInstantiation){
                    ItemToInventory();
                    Destroy(col.gameObject, 0.4);
                    isSprite = true;
                }
                isSprite = true;
            }    
        }
    }    
    else {
        anim.SetBool("isIdle", true);
        isSprite = false;
        isPickUp = false;
        isMoney = false;
     }
}
function PickUpMoney() {
    var moneyText = GameObject.FindGameObjectWithTag("Bank").GetComponent(Text);
    var moneyAmount : int = Random.Range(5,10);
    currentAmount += moneyAmount;
    moneyText.text = "₳ " + currentAmount;
}
function ItemToInventory() {
    itemSprite = Instantiate(slotPrefab) as GameObject;
    itemSprite.transform.SetParent(inventory.transform);
    itemSprite.AddComponent(Image).sprite = spriteImage;
    itemSprite.tag = spriteTag;
    itemSprite.name = spriteName;
}
1 Like

Ah great! It works. You saved me a day of frustration! Thank you master roykoma! :smile: