Individual Stats for gameobjects in a foreach loop

Hey guys,

I’m having trouble with the foreach loop and quiet can’t find a way to fix it. So the problem is that I’m having a foreach loop for some gameobjects. These gameobjects have 3 different bools where only one can be true. So the foreach loop says for all gameobjects do thing 1 if bool 1 is true, to thing 2 if bool 2 is true and do thing 3 if bool 3 is true. Now the script should only do one thing but if I have 3 gameobjects that all have different bools that are true and I want to run the script then the script does all three things because all gameobjects combined might have all bools set to true. But I don’t want that to happen. So is there a way to differentiate these objects in some way?

Appreciate any answers

I think you need to post parts of your code to get help on this one.

I agree some code might help. But I don’t really understand what you are saying.

You have several gameobjects with different bools. Do you need each gameobject to trigger something different or you need 1 thing to happen and all bools should be the same?

If you are triggering something different on the gameobject depending on the bool. You just need to check which bool is true and then trigger on that gameobject within the foreach loop. Then it will only execute on whatever gameobject you are on at the time.

here is my two cents

public GameObject[] listOfGameObjects;
private bool setting1;
private bool setting2;
private bool setting3;

private void checkBools()
{
    foreach(GameObject tempObject in listOfGameObjects) //the main loop for checking the bools of each gameobject
        {
            if(tempObject.bool1 == true)
            {
            setting1 = true; //set the local bool to true
            }
            if(tempObject.bool2 == true)
            {
            setting2 = true; //set the local bool to true
            }
            if(tempObject.bool3 == true)
            {
            setting3 = true; //set the local bool to true
            }
        }

//check the local bools
    if(setting1 && !setting2 && !setting3)
    {
     //do something, only bool1 is true
    }
    else if(!setting1 && setting2 && !setting3)
    {
     //do something, only bool2 is true
    }
    else if(!setting1 && !setting2 && setting3)
    {
     //do something, only bool3 is true
    }
}

Thanks for the answers so far. Sorry that i didnt put in code.

public void SetTarget(MouseOver _target)
    {
        target = _target;

        transform.position = target.GetBuildPosition() + 2*Vector3.forward + 3*Vector3.up;

        GameObject[] cubes = GameObject.FindGameObjectsWithTag(cubeTag);
        foreach (GameObject cube in cubes)     
        {
            MouseOver _m = cube.GetComponent<MouseOver>();

            if (_m.StandardTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    StandardTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    StandardTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    StandardTurretLevelOne.SetActive(true);
                }
            }

            if (_m.SniperTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    SniperTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    SniperTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    SniperTurretLevelOne.SetActive(true);
                }
            }

            else if (_m.CannonTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    CannonTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    CannonTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    CannonTurretLevelOne.SetActive(true);
                }
            }             
        }
    }

So what it does is that it shows different UIs for different turrets. But here comes the problem. The turrets are built on the cubes in the foreach loop and if i have lets say 3 cubes, one with a standard turret built on it, one with a sniper turret and one with a cannon turret, then all three if statements are true in my foreach loop and then ingame all the 3 turret UIs show up if i click one turret. But i obviously only want the correct UI to show up. Is therer a way to do that or is my script just crap?

Thanks for the help

Part of the issue may be you want to use if else and not just if checks.

However, if you have three cubes and each has a different turret, you should only be triggering the one that is true.

Oh wait, I think I understand. You are clicking on a turret to display some info, but you are looping through all turrets when you do that? Why are you doing that? Is that what you are doing?

Yeah this is kind of what I’m doing and while reading your answer I might know why this doesn’t work. The information is sitting on the cube where the turret is built on and not the turret itself. I was able to let each cube know what kind of turret is built on this cube but now I want to display the right UIs. So no matter what cube I’m clicking I want, depending on what turret sits on the cube, display the right UI on it. So my idea was just to read this information from each cube but this obviously doesn’t work the way I do it.

Yes, if you are displaying some sort of UI, you don’t want to loop through all turrets. If I click on a turret, I want only that turrets info. So you’ll need to turn on or off stuff based on that turret only and not all turrets in the scene.

1 Like

Ah nice I see. Thanks for the help already now I clearly know why my version doesn’t work. Do you have any advice how i could do that better? Because I’ve been with this problem for quite a while and don’t really know what else I could do or where else I could loop through if it is even necessary.

WIthout knowing more about your setup, I’m just going to guess. But you’ll probably want a script on your turrets that receives mouse clicks/touches and then can turn on the proper ui. If it’s just a single set of ui that you use for all turrets, just move it over and check the values for that turret to determine what you want to turn on.

I don’t think you’ll need to loop through unless you are doing a mass upgrade. If you had a building where you could purchase upgrades such as all sniper towers get longer range, but then you’d probably just have some base stats that you track and update for something like that (towers would track upgrade stats/level etc that are unique to them).

question for you,
what script or how does the MouseOver object get sent to this script? is this the current object that the mouse is over?
But again we are guessing because we don’t have the full code and full details of the game
public void SetTarget(MouseOver _target)

if it is then you should be able to use it.

public void SetTarget(MouseOver _target)
    {
        target = _target;
 
        transform.position = target.GetBuildPosition() + 2*Vector3.forward + 3*Vector3.up;
 
        //GameObject cube = target.transform.gameObject;  not needed

            //MouseOver _m = cube.GetComponent<MouseOver>(); //you already have this from target
 
            if (target.StandardTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    StandardTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    StandardTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    StandardTurretLevelOne.SetActive(true);
                }
            }
           else if (target.SniperTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    SniperTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    SniperTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    SniperTurretLevelOne.SetActive(true);
                }
            }
            else if (target.CannonTurretBuilt == true)
            {
                if (target.isUpgradedOne == true)
                {
                    CannonTurretLevelTwo.SetActive(true);
                }
                if (target.isUpgradedTwo == true)
                {
                    CannonTurretLevelThree.SetActive(true);
                }
                if (target.isUpgradedOne == false && target.isUpgradedTwo == false)
                {
                    CannonTurretLevelOne.SetActive(true);
                }
            }        
   
    }
1 Like

Johne5 thanks a LOT for that. That just solved my problem. I was just thinking about how to get the component for each gameobject without looping anything but as you mentioned in your comment this isn’t even needed due to the fact that i already have this component from “target”. Again thanks a LOT for helping me :slight_smile: