Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Checking if multiples bools are true

Discussion in 'Scripting' started by alexandre_parent80, Apr 3, 2020.

  1. alexandre_parent80

    alexandre_parent80

    Joined:
    Mar 30, 2020
    Posts:
    5
    Hey I'm somewhat new to Unity and I work on a small game. I got multiple items with bools to specify what material they are made with. I instantiate them randomly from a array for the player to catch them and put into a empty array. All work so far but how can I check if they got the required material before it go in the empty array?

    Something like if(itemMadeWith == requiredMat)

    Script on each item:
    Code (CSharp):
    1. public class Items : MonoBehaviour
    2. {
    3.     [Header("Item made with")]
    4.     public bool copper;
    5.     public bool iron;
    6.     public bool gold;
    7.     public bool silver;
    8.     public bool graphite;
    9.     public bool lithium;
    10.     public bool nobium;
    11.     public bool titanium;
    12.     public bool nickel;
    13.     public bool zinc;
    14.     public bool salt;
    15.  
    16.     [Header("Item details")]
    17.     public string itemName;
    18.     public string description;
    19.     public int value;
    20.     public Sprite itemSprite;
    21. }
    Instantiated like this:
    Code (CSharp):
    1.  
    2. public class SpawnItems : MonoBehaviour
    3. {
    4.     public static SpawnItems instance;
    5.     public GameObject[] itemsToSpawn;
    6.     public int xPos;
    7.     public bool spawnItems = false;
    8.    
    9.     void Start()
    10.     {
    11.         instance = this;
    12.     }
    13.     public void StartGame(int level)
    14.     {
    15.         StartCoroutine(NewItem(level));
    16.     }
    17.     IEnumerator NewItem(int level)
    18.     {
    19.         spawnItems = true;
    20.         while (spawnItems)
    21.         {
    22.             xPos = Random.Range(1, 13);
    23.             Instantiate(itemsToSpawn[Random.Range(0, 4 + level)], new Vector3(xPos, 8, 0), Quaternion.identity);
    24.             yield return new WaitForSeconds(1f / level);
    25.         }
    26.     }
    27. }
    28.  
    And this is how I catch them to put in an empty array:
    Code (CSharp):
    1.  
    2. public class CatchedItem : MonoBehaviour
    3. {
    4. private void OnTriggerEnter2D(Collider2D other)
    5.     {
    6.         Debug.Log("Collision Detected " + other.gameObject.name);
    7.         if (other.gameObject.name == "Trigger")
    8.         {
    9.             GameManager.instance.AddItem(GetComponent<Items>().itemName);
    10.             Destroy(gameObject);
    11.         }
    12.  
    13.         if (other.gameObject.name == "Colision Box Floor")
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.     }
    18. }
     
  2. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    72
    Since you're using multiple bools, you'd have to do something like
    if (requiredMaterial == "zinc" && item.zinc == true || requiredMaterial == "iron" && item.iron == true || etc etc etc)


    I recommend you get rid of all those booleans and use an enum instead.

    Code (CSharp):
    1. public enum ItemMaterial
    2. {
    3.    Zinc,
    4.    Copper,
    5.    Iron,
    6.    etc...
    7. }
    Now in your item you can have a
    public ItemMaterial[] materialsMadeOf
    array and your player's "required materials" should also be an ItemMaterial[] array.

    Now you can do something like

    Code (CSharp):
    1. foreach (ItemMaterial material in item.materialsMadeOf)
    2. {
    3.     foreach (ItemMaterial required in player.requiredMaterials)
    4.     {
    5.         if (required == material)
    6.         {
    7.                // Add item
    8.                return; // We already added the material, no need to keep looping.
    9.         }
    10.     }
    11. }
    Keep in mind I wrote this code in the reply, there could even be syntax problems, this is just so you get the idea.
     
    alexandre_parent80 likes this.
  3. alexandre_parent80

    alexandre_parent80

    Joined:
    Mar 30, 2020
    Posts:
    5
    Martinlsla do I ad the foreach inside the OnTriggerEnter2D? It give me some error because its not in the same script as my Items script.
     
  4. alexandre_parent80

    alexandre_parent80

    Joined:
    Mar 30, 2020
    Posts:
    5
    Nvm figured the problem. It work great with enum. Thanks!
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    2,421
    I assume only one of these bools can be true at a time? Determining the identity of item like this doesn't really make sense. If it works like I think it does, you might consider using an enum value instead of a bunch of bools.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum

    Alternately, just use the item name. so instead of
    if (requiredMaterial == "zinc" && item.zinc == true || requiredMaterial == "iron" && item.iron == true || etc etc etc)

    you could simply write:
    if (requiredMaterial == item.itemName)
     
    Last edited: Apr 3, 2020
  6. MartinIsla

    MartinIsla

    Joined:
    Sep 18, 2013
    Posts:
    72
    Glad it worked! Good luck with your game
     
unityunity