What does this error mean? An instance of type '' is required to access non static member

Good day,

I was trying various methods to open a door with a key (or any object for that matter) that has to be picked up to unlock.

After a lot of research, and working around, I have the following Scripts:

I have created a new Gameobject(renamed to Key), and add the object to pick up as child. I then add a box collider to the Gameobject, and make it a trigger ,as well as the script below. So when the player is in range of the box collider, it should pick up the key.

Keypickup.js

private var doorKeyOpens : Transform;
private var gotKey : boolean = false;

function HasKey() { return gotKey; }

function OnControllerColliderHit (hit: ControllerColliderHit) {

 if(hit.gameObject.name == "key") {
    //Debug.Log('key picked up'); print("You Picked Up The Key");
    OpenDoorwithPassword.gotKey = true;
    Destroy(hit.gameObject); 
 } 
}

I also have another Gameobject with a door attached to it. The Gameobject have the following code to open when the player is in range, and have the key

OpenDoorwithPassword.js

var door: Transform;
var gotKey : boolean= false;
var angleOpen: int;
var angleClose: int;
var speedOpen: int = 100;

function OnTriggerStay (other: Collider) {
    var player : Keypickup = other.gameObject.GetComponent(Keypickup) as Keypickup;
    if(player != null && player.HasKey()) {
        if(door.transform.localEulerAngles.y < angleOpen) {
            door.transform.Rotate(Vector3.up*Time.deltaTime*speedOpen);
        }
    }
}

I then get the following debug error, and I am not sure what it mean?

Keypickup.js(13,26): BCE0020: An instance of type ‘OpenDoorwithPassword’ is required to access non static member ‘gotKey’.

any help will be appreciated.

Best regards,

Jakes

Hey there, and welcome to Unity Answers!

The error you’re receiving is because you’re trying to access an instance method, without actually having an instance of your class. This probably sounds like gibberish, so I’ll give an example. Let’s say you have a “Car” type that has a “StartEngine” method. Since there are potentially lots of cars driving around, if you just try to say, “Car.StartEngine()”, the compiler won’t know which car you’re talking about (and you’ll get the above error). Instead, you would need to call StartEngine() on a specific car: “littleMilkTruck.StartEngine()”.

Now the problem is this: how can you get an instance of OpenDoorwithPassword? There are a couple of approaches you can use:

  1. Is the OpenDoorWithPassword script attached to an object in your scene? If so, you can get it using these techniques: http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
  2. Is there only even one OpenDoorWithPassword instance in your scene? Do you want to have easy access to it from multiple places in your game? If so, the “Singleton” pattern might be fun and interesting to use. A “Singleton” essentially represents a class that will only ever have one instance, and provides a quick and easy way to get that instance from anywhere. If you’d like to go this route, see here: javascript singleton - Questions & Answers - Unity Discussions

Good luck!

Good day, Thank you for the prompt reply.
W will go with option 1, as I would like to have more than one door, and each one will have their own key.
I will study the option you gave, and reply once I made progress.

Best regards