Need help with a script to unlock the exit when having x points

So basically I have a script on a door to when im in his collision box it will load the next level, this is the script:

#pragma strict

function OnTriggerEnter(Col : Collider)
{
if(Col.tag == “Player”)
{
Application.LoadLevel(“Level 1”);
}
}

And I also have a script that will tell how many keys ( cause i choose keys to be the score) I have heres is the script:

#pragma strict

static var keyCounter = 0;
function Start () {

}

function Update () {

}

function OnCollisionEnter(collision : Collision)
{
if (collision.transform.name == (“Key”))
{
keyCounter += 1;
}
}

Also have a script to destroy the object but i think its not needed for this and i would like to know how could i make to only be able to enable the first script(the one to go to the next level) just after i get an choosable ammount of points.
Thanks for your time reading this, any help would be apreciated.

please use code tags when you paste code into the forums, there is a sticky on them at the top of the forum.

Also when you paste in unityscript we can’t see the file names, always good to know.

Have a look at GetComponent and Find, you need to get a reference to the second script in the first script. You can then check the keyCounter from the first scripting using that reference.

I Didn’t know it first time using the forum, i dont understand nothing about scripting but you are recomending me to use the GetComponent on the exit script to have the keyCounter … i really dont understand nothing of scripting, i’ve been searching in the internet for this and i found 2 similar questions but i couldnt make it work and nobody said that it actually worked so i dunno here is the thread: When all Coins are collected, an exit level object appears. - Questions & Answers - Unity Discussions
this is the exit script called end.js:

#pragma strict
function OnTriggerEnter(Col : Collider)
{
    if(Col.tag == "Player")
    {
        Application.LoadLevel("Level 1");
    }
}

This is the player script called PlayerCounter.js:

#pragma strict

static var keyCounter = 0;
function Start () {

}

function Update () {

}

function OnCollisionEnter(collision : Collision)
{
if (collision.transform.name == ("Key"))
{
keyCounter += 1;
}
}

Have you done any of the tutorials? They will explain how to do a lot of this.

If the player is keeping track of the score you can put the collision detection into the player then check if he has collided with the exit trigger. Inside that collision detection check if the number of keys is what you want. That way you won’t have to call the player script from the exit. It’s not as neat & you won’t learn how to call other scripts tho, plus your player script could end up very long & unwieldy if you do it a lot for all different things.

Yes, i have done the tutorials but all they teach is how to get the score or how to exit to the next level, they dont teach what i want, the collision detection you are talking about is the PlayerCounter.js right if yes its already on the player components.
i dont really understand the “Inside that collision detection check if the number of keys is what you want.”
sorry if im really dumb in this …

The way you get the score from one script into another is the same way you get other things, just reference different scripts/objects.

On the player, have ontrigger check if the player hits the exit. If it does do an if statement checking the # of keys & then do whatever you want from there. I can’t do Java but basically copy the code you have in the exit, paste it into the player & where you are checking the name of the collider change it to exit or whatever you have called it then also add the check && key counter>= x then load the next level.

i didnt understand that check && key counter>=x part cuz it requires some knowledge and i really shouldnt be doing this with out getting to know first the basics, but i thought that i will only do stuff that are on tutorials but i really wanna do this project so … what i did was this:

#pragma strict

static var keyCounter = 0;
function Start () {

}

function Update () {

}

function OnCollisionEnter(collision : Collision)
{
if (collision.transform.name == ("Key"))
{
keyCounter += 1;
}
    Application.LoadLevel("Level 1");
    }
function OnTriggerEnter(Col : Collider)
{
    if(Col.tag == "Finish")
    {
        if (keyCounter>= 2);
            {
            Application.LoadLevel("Level 1");
            }
    }
}

now when the player touches the ground it goes to the next level …

ok im dumb… i just removed the line 18 … and it works i guess

Update: Not really the player goes to the next level even if the keycounter is under 2

Double Update: it works with the script like this:

#pragma strict

static var keyCounter = 0;

function OnCollisionEnter(collision : Collision)
{
if (collision.transform.name == ("Key"))
{
keyCounter += 1;
}
    }
function OnTriggerEnter(Col : Collider)
{
    if(Col.tag == "Finish")
    {
        if (keyCounter>= 2)
            {
            Application.LoadLevel("Level 1");
            }
    }
}

Thank you guys for helping me in my problem :smile:

Well done. A tip, don’t start making the game you really want at the start as it will never be as good as you want it to be because you don’t have the skills. That said, certainly learn to do the basic mechanics now, just don’t expect everything to be perfect or efficient.

As I said, I don’t know Java but you should be able to check for multiple conditions in the one if statement so instead of saying if the collider is finish & then checking if the player has the right number of keys you should be able to say if the collider is the finish & the player has the right number of keys…
In c# it would be
if(col.gameobject.tag==“Finish” && keyCounter >=2){
Application.LoadLevel(“Level 1”);
}