Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question system keys and inventory

Discussion in 'Editor & General Support' started by liccardi, Nov 1, 2022.

  1. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    16
    hello everyone and thanks in advance for the help , I'm trying to manage the key system and opening the doors , the keys I manage through scriptableObject , so each key has its id , the red key has an id 0 , the yellow key has an id 1 and so on , but how could I say that the yellow key can only open the yellow door , Can the red key only open the red door and so on for every key and door I have? I'm going crazy :mad::oops:, I attach some images
     

    Attached Files:

  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Don't even need the ID. You can check Unity object's for equality with
    ==
    .

    Just make a public field for one of your key SO's on your door script component, and a assign the key that should unlock it.

    Then when they try to open the door, run down the player's inventory and check if a matching item is in their inventory. Eg:
    item == unlockKey
    .

    Obviously there's more code to it than that, but I'm just illustrating how you would use these SO's like this.
     
  3. liccardi

    liccardi

    Joined:
    Dec 6, 2018
    Posts:
    16
    excuse me, my English is not very good, are you telling me that I have to make a new SO only for the door and assign it to each door?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,008
    Not quite. What I'm saying is that since you can reference and assign scriptable objects in the inspector, you can just check for the right SO by checking if one in the player's inventory is the same one referenced by a door's script component.

    The same scriptable object can be referenced in multiple places.