Door requires certain score to open. Clicking on door before score is met gives +1 score.

Hello, I am creating game in which you have to collect certain items to open a door which loads another scene. This is my first standalone project and new to scripting a lot of it is patch work.

In the level you need to find 4 items to open a door. If you try to click on the door with the score lower then 4 it will not open.

My problem is that if I click on the door it gives me +1 Score.
Even if I have a score of 0 after clicking the door it will give me +1.

if i keep clicking on the door i can get a total of 4 and be able to leave

The items that you need to pick up has a Score.js on it which gives the score, but the door does not, it has a level transfer script on it.

Is there any obvious solutions that I am not seeing?


Score.js

public static var score = 0;
var scoreText = "Fragment: 0/4";
var mySkin : GUISkin;
var CanEnter:boolean = false;
      
function OnTriggerEnter(coll:Collider){
    CanEnter = true;
}
 
function OnTriggerExit(coll:Collider){
    CanEnter = false;
}

 
function Update(){
     if(Input.GetKeyDown("e") && CanEnter){
    Debug.Log("OnTriggerEnter() was called");
        Debug.Log("Other object is a coin");
        score += 1;
        scoreText = "Fragment: " + score + "/4";
        Debug.Log("Score is now " + score);
        CanEnter = false;
        
    }
}
 
function OnGUI () {
    GUI.skin = mySkin;
    GUI.Label (Rect (10, 10, 500, 200), scoreText.ToString()); }

door code.

var CanEnter:boolean = false;
 
function OnTriggerEnter(coll:Collider){
    CanEnter = true;
}
 
function OnTriggerExit(coll:Collider){
    CanEnter = false;
}
 
function Update(){
    if(Input.GetKeyDown("e") && CanEnter && Score.score == 4){
       Camera.main.SendMessage("fadeOut");
       Invoke("LevelLoad", 2);
       Application.LoadLevel ("Hallway");
       
    }
}
function LevelLoad()
{
	yield WaitForSeconds (40);
	Application.LoadLevel ("Hallway");
}

Several comments:

  • I don’t see anything in your script(s) that deals with clicking on the door? Is there are another script that you forgot to include that deals with mouse input, or did you actually mean “press e next to the door”?
  • In Update() of Door.js, you Invoke “LevelLoad”, which does Application.LoadLevel(“Hallway”) after a wait of 40 seconds, but then you call Application.LoadLevel(“Hallway”) in Update immediately after anyway…?
  • You’ve got “CanEnter” variables attached to both the door itself and also to the items that you pick up to allow you to open the door. Duplication like this almost always means you’ve got your design wrong somewhere. In this case, I’d say that it’s the Door object itself that should determine whether it can be entered - get rid of the “CanEnter” logic from Score.js
  • While we’re on the subject of design, it looks like you’ve got rigidbody colliders attached to the door and the pickups, whereas you’ve got a trigger attached to the player themselves. I’d normally do the opposite, which means that you don’t have to have OnTriggerEnter() / OnTriggerExit() functions in every separate object with which the player can interact - instead, you have one set of OnTriggerEnter() / OnTriggerExit() functions on the player object themselves, and then test what sort of object the player just triggered (using collider.gameObject.tag == “whatever”) to see whether it was a door, coin, weapon etc. and act accordingly.