Trying to get 2 script bool to affect each other depending on which bool of the array list is true

I want to make a list of bools and the InteractableList will will allow me to just set one of the bool to true which will determine which object it is, and then in the Interactions script when I (currently) move over that object, which will determine which object it is, set that specific bool to true in my interactions script (meaning I have collected that specific object), and delete the object. What I need to know is how would I determine which object in the list is it (I set it to true in the editor) and then how would the other script determine which bool is true in the list according to the editor, determine the bool they are related? to (I want element 0 in InteractableList to affect hasKnife in Interactions).Currently my scripts are

195131-screenshot-2022-04-17-110021.png

Hey there and welcome to the forum,

not completly sure where you are heading with this but from the information given i’d strongly suggest to take 2 steps back and to choose a slightly different approach. Having something like and array of bools might look like an easy solution at first but can get quite tiresome as soon as you have more objects like 10-20 onwards.

So given this let’s take a look at some techniques that can be used to deal with this:

First up check out Enumerations

   // this "enum" type can be used to create a List of "Names" that translate into numbers which can be use as indexes.
   public enum ItemType { None, Knife, Pencil, Gun, Shield, Sword };
   
   //if you now give this variable to a script you can set the "Type" in the inspector from a Dropdown menu.
   public ItemType type;

enums can help to make your code really readable as you can have comparisons like this:

  if(type == ItemType.Knife)
        //do something because this is a knife.

Another thing that you should look into is Inheritance.

For example you could create a class:

   public class CollectibleItem : MonoBehaviour
   {
         public virtual void Use() {} //this here can stay empty as the base class does not need behaviour here. It is only there to create the "structure" of an item.
   }

On this you can now build Items which are derived from the CollectibleItem base class: (each of these have to go into their own file like if you create a new script for any Monobehaviour)

   public class Knife : CollectibleItem {
        public override void Use() {
             //do whatever a knife should do specifically if you "Use" this item.
        }
   }

  public class Pencil: CollectibleItem {
        public override void Use() {
             //do whatever a pencil should do specifically if you "Use" this item.
        }
   }

What can we use this for? If you have a player script you can now do the following:

  public class YourPlayerScript : Monobehaviour {
         // List for Collectible Items:
         public List<CollectibleItem> inventory = new List<CollectibleItem>();  

         public void OnCollisionEnter(Collision col)  
         {
              var collectible = col.gameObject.GetComponent<CollectibleItem>();
              if(collectible == null)
                   return; // do nothing if this is not a collectible
              //if we are here then it is a collectible so we can do something with it:
              //for example use it:
              collectible.Use(); //this will now call the "Use" function. This will always call the "override" behaviour from the derived classes like "Knife" or "Pencil". 
              //or add it to our Inventory:
              inventory.Add(collectible);
              //or check if it is a specific collectible:
              if(collectible is Pencil)
                      //do something specific since it is a pencil
       }
 }

As shown above you can use this class inheritance base to easily handle different objects in a generalized manner. Most importantly though, you can easily add more. Just add a new derived class from CollectibleItem. It will automatically work with your inventory, you can easily give it its own behaviour.

Let me know if something was unclear here. I know that this might be a bit much since you are still at the start of your journey but i can only encourage you to try and understand these methods/principles as they will save you a lot of pain further down the road.

I don’t currently have Unity open, but I will try that in a second.

BTW It’s a detective game so the plan is you talk with people, and then it will unlock certain chatting interactions once you get specific items, which my plan was just that if some specific “has” bools were true it would allow me to progress in the game

@Captain_Pineapple so it might work, not fully sure. What I am trying to do is where I walk over an object, and then it checks in the editor which object it is. After it determines which object it is (let’s say knife), it will then destroy the object (which is pretty easy) and set the bool related to that object (not code-wise) to true. The bool “hasKnife” should be set to true when you walk over the item that has a script that in the editor the enum is set to Knife. Sorry if this is not enough info but I am currently out of time.
195200-screen-shot-2022-04-18-at-32504-pm.png