2D need help with picking up a key to unlock a door to lead to new scene in C#

Hello, I’m still new to learning and understanding C# and really don’t know all the technical terms if it isn’t explained to me in a way I can understand. I’ve watched and read countless tutorials on this subject, but no matter what, none of them fully worked for me. This is a 2D project and not a 3D one. I’ve got my “KeyPickup” and “BossLevel” scripts kinda made. However at the moment I can just pick up the key and or enter the door. So there is no registering yet that I need the key in order to enter the door; which is what I don’t understand how to do.

Here is the basic key pick up script.

using UnityEngine;
using System.Collections;

public class KeyPickup : MonoBehaviour {

    public AudioSource keySoundEffect;

    public virtual void OnTriggerEnter2D (Collider2D collider){
       
        if (collider.GetComponent<CharacterBehavior> () == null)
            return;
       
            keySoundEffect.Play ();
       
            gameObject.SetActive (false);
       
    }

    public virtual void onPlayerRespawnInThisCheckpoint(CheckPoint checkpoint, CharacterBehavior player)
    {
        gameObject.SetActive(true);

    }
}

Here is the basic script I am using to go to the next scene, but I am eventually wanting it to require the key in order to continue.

using UnityEngine;
using System.Collections;

public class BossLevel : MonoBehaviour {

    public string LevelName;

    public virtual void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.GetComponent<CharacterBehavior>()==null)
            return;
            GoToNextLevel ();

    }
   

    public virtual void GoToNextLevel()
    {
        LevelManager.Instance.GotoLevel(LevelName);
    }
}

With that being said, I have tried making a “KeyInventory” script with a bool attached to the player with “KeyInventory.hasKey = true;” in the keypickup But, when trying it this way it gave me an error “Assets/KeyPickup.cs(13,30): error CS0120: An object reference is required to access non-static member `KeyInventory.hasKey’” and wont let me do anything until I fix it. So for the time being I took it out of my script.

Someone please help me understand how to do this better.

Use PlayerPrefs for this purpose. Google for more info.

Basically you should add in your pickUp_script:

PlayerPrefs.SetString("Key", "BossKey");

and on boss level:

if (PlayerPrefs.GetString("Key") == "BossKey") {
//if there are a Key with value BossKey then open the door

}

P.S: I didnt test it ofc, just out of my mind… but you got the clue where to continue

You can just use bools for each key the player has. Set them to false on the player & then change the relevant one to true as they collect the key. Have a trigger at the door that will only open the door if the relevant bool is true.
If they are collecting the key in one scene & needing to use it in another (not to get to another but to actually get the key, go to a new scene, & then need to use the key) you will need to use player prefs to set the bool & then retrieve it when you get I the next scene.

Thanks. I don’t fully understand the player prefs thing. I did try it, but nothing changed and still could enter door w/out grabbing the key.

I understand the idea behind using bools. I did try that before when I made the KeyInventory thing, but I couldn’t get that to work properly either.

OK, let’s break it down first so we get a clear picture.

Is the door in the same scene as the key?

Yes the door is in the same scene as the key.

Ok.

  • Make a public bool on the player set to false.
  • Set it to true when the player collects the key.
  • On the door script create a public game object for the player reference & also a public reference to the player script where the bool is.
  • When the player collides with the door check if the bool in the player script is true.

Apparently I don’t understand C# enough to do this. I keep getting errors. So I got this attached to player.

using UnityEngine;
using System.Collections;

public class KeyInventory : MonoBehaviour {

    public bool hasKey;

}

Then I added that it was true on pickup… but apparently I am completely not understanding it.

using UnityEngine;
using System.Collections;

public class KeyPickup : MonoBehaviour {

    public AudioSource keySoundEffect;

    public virtual void OnTriggerEnter2D (Collider2D collider){
       
        if (collider.GetComponent<CharacterBehavior> () == null)
            return;
           
            KeyInventory.hasKey = true;

            keySoundEffect.Play ();
       
            gameObject.SetActive (false);

    }

    public virtual void onPlayerRespawnInThisCheckpoint(CheckPoint checkpoint, CharacterBehavior player)
    {
        gameObject.SetActive(true);

    }
}

I keep getting this error.
Assets/KeyPickup.cs(13,38): error CS0120: An object reference is required to access non-static member `KeyInventory.hasKey’

I’ve tried to add a public KeyInventory keyInventory; and changing the area of hasKey to true to lowercase. Then I get this error when I go to pick up the key.
NullReferenceException: Object reference not set to an instance of an object
KeyPickup.OnTriggerEnter2D (UnityEngine.Collider2D collider) (at Assets/KeyPickup.cs:15)

Please break this down and explain to me what I am doing wrong.

Ok, I’m sorry but I don’t have time. We just showed the beta of our game & we are ramping up due to a positive reception, the global game jam is on this weekend & we are getting stuff ready for that, & class goes back next Monday so I’m also reviewing the stuff from last year as a refresher.

I’d say you are trying to run before you can walk so I’d suggest that you put this project aside & start with the tutorials in the U its learn section. They will help by showing how to do this & explaining why it needs to be done that way.

Sorry again