• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by RedBlackGold · Apr 17 at 06:09 PM · scripting beginner

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

alt text

alt text

screenshot-2022-04-17-110042.png (33.1 kB)
screenshot-2022-04-17-110021.png (17.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Captain_Pineapple · Apr 17 at 08:32 PM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RedBlackGold · Apr 17 at 09:52 PM 0
Share

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

avatar image RedBlackGold RedBlackGold · Apr 17 at 10:05 PM 0
Share

@Captain_Pineapple I tried doing that, and I much prefer the enum over the boolean list, although now I am wondering how do I call determine which type it is in a different script. Also if that's the purpose of inheritance, could I get a bit more of information about it, as I don't understand whether I am supposed to create a new script, or just make another class in that class (which I don't think works, but not sure as I don't remember), nor how it would allow me to use a enum from a different script in my interactions script. EDIT: Oh lol didn't mean to put this as a reply to my own comment

avatar image Captain_Pineapple RedBlackGold · Apr 18 at 12:01 PM 0
Share

No worries about the comment.

Not completly sure if i understand your problem. If you want to read a small tutorial to sum up inheritance you can for example check out This Link (Easy example), or This Documentation (more complex in-depth).


If you create any class that is a Monobehaviour and you want to use it as a Component on a GameObject then each class has to go into its own file.

So if you have your CollectibleItem that needs to be in "CollectibleItem.cs" and your Knife would be in "Knife.cs". Otherwise Unity will not find the classes.


To solve the issue about "Having" a certain Item to continue there could be multiple ways to deal with this. Either: Give your Knife class a variable of ItemType and select it as Knife so you basically have a secondary way to check for the "Type" of your Item. Or: Give your Collectible Item a function OnAddToInventory that you call when you pick it up. Then you can use this to code which type of item should unlock what actions in the game when picked up.

avatar image
0

Answer by RedBlackGold · Apr 18 at 10:27 PM

@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. alt text


screen-shot-2022-04-18-at-32504-pm.png (59.8 kB)
Comment
Add comment · Show 16 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Apr 19 at 06:58 PM 0
Share

okay, not sure what the "Checker" class is good for but some notes to the code you posted. in general the code has to be in some function of the class, otherwise it will not be viable.


Next thing is that variables that are private, will not be accessible from child classes.

     //these are private and only available in the same class:
     private float someValue;
     float someValue;

     //this is like private but makes it also available for child classes:
     protected float someOtherValue;

     //this is available everywhere:
     public float somePublicValue;

So currently hasKnife in InteractableList must at least be protected instead of private. If you have any more questions regarding this please add a bit more info.

avatar image RedBlackGold Captain_Pineapple · Apr 19 at 09:58 PM 0
Share

I honestly don't know why I made the checker, but hasKnife is in the Interactilbles script. Uhm ok, so my goal is that I have an Interactions script on the player (It will end up with a button, it checks the tag of the object you are interacting with and then does a certain thing depending on the tag). I need another script called InteractableList which I put not on the player but specifically on collectible objects and NPCs, and this script is supposed to determine which specific object/NPC it is. Once it has been determined that it gets sent to the Interactions script when they interact with the specific object so with objects it will check which object it is in the InteractibleList (for example with a knife it will see that the enum is a knife), and then that's gets sent to the Interactions script on the player and will set the hasKnife bool to true, and this bool is used for when you talk to NPC's, and in a basic version of the game it's if you do not have the knife you get answer 1 where you don't win, but if you have the knife and talk to the NPC then you get answer 2 in which you win.

This can't be done in 1 script because if I end up giving the script onto the objects and NPC's then the knife for example is going to be like "oh, there's a knife on me. Time to delete it and set the bool to true" but that would set the bool on the knife (which has now been deleted) to true, making it impossible to set to true on the player as the object has already been deleted.

avatar image Captain_Pineapple RedBlackGold · Apr 20 at 05:45 PM 0
Share

Okay, i do not really understand what your Interactable List is for.

In my mind i'd try to build a system like this:


My Player Script has a list of flags:

   //bools for all possible item enum values:
   private bool[] itemFlags = new bool[System.Enum.GetNames(typeof(ItemType)).Length];

this is a list that automatically contains as many entries as there are values in the enumeration ItemType. Then my player can interact with an item for example by using a raycast or any other method that returns me a GameObject interactGameObj that i want to try to interact with.

 //get the collectible refrence:
 var collectibleReference = interactGameObj.GetComponent<CollectibleItem>()
 if(collectibleReference != null) //check if it is != null
 {
         //set the flag that we have the item to true:
         itemFlags[(int)collectibleReference.type] = true;
         //destroy the interacted item:
         Destroy(interactGameObj);
 }

This way you now have an array of flags that you can check if you for example have a knife:

    if(itemFlags[(int)ItemType.Knife]) // if this returns true we have a knife.

Does this make sense? Is this what you are trying to do?


Sorry if this seems a complicated way to do things but imo it is important to not have single flags for things like "i have item X" since this will get out of hand the more items you have. Systems should always be scaleable.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if my player has reached a certain position? 2 Answers

Tracking UI button presses 1 Answer

When my player dies and i hit revive button i want my player back to the game but i m unable to do that please help. 2 Answers

is same code written in less lines, more beneficial ? 3 Answers

how to apply force on correct direction in wall jump using Rigidbody 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges