Javascript collision counter to go up by 1

Hi there,

This is my first post and I am new to javascript.

I have my character object and when it lands on an object I want the counter to go up by 1 each time, but also not destory that object it needs to remain there.

Here is my script for the counter

static var Counter: int = 0;

function Update () 
{

    guiText.text = "Platforms: "+Counter;
   

    }

Here is my script that is assigned to the character.

function Update(){
    if(platformCounter.Counter >= 1000)
    {
       Application.LoadLevel(0);
       platformCounter.Counter = 0;
    }
}

function OnControllerColliderHit(hit : ControllerColliderHit)
{
    if(hit.gameObject.tag == ("platformCounter"))
    {
       platformCounter.Counter = 1;
       hit.collider.gameObject.active = true;
       
    }
}

So far the script will get to 1 but will not increase any more. I have searched forums and the unity boards for help.

I have a feeling the problem is to do with the script on the character…

platformCounter.Counter = 1;

Cheers :slight_smile:

I think that you should try :

platformCounter.Counter += 1;

or

platformCounter.Counter = platformCounter.Counter + 1;

because at the moment, you are setting Counter value to 1 each time there’s a collision, it’s not incrementing.

Thanks for the quick reply SmokyZebra,

Yeah I used to have the += in the code so it would increment but it increments too fast and just add’s up the score really quick. and then the game will end when it gets to 1000.

I basically need it so when the character lands on platform 1 it shows a score of 1 and when the character lands on the next platform it shows a score of 2 etc. hmmm

In that case, you need to have a script on each platform, with a condition so the counter go up by only one and not keep growing as the collision continue.

so that would looks like to this :

script on your guitext named PlatformCounter:

    static var Counter: int = 0;
     
    function Update ()
    {
     
        guiText.text = "Platforms: "+Counter;
        if(Counter >= 1000)
    {


       Application.LoadLevel(0);
       Counter = 0;

    }
       
     
}

And a script on each platform, not on the character :

var CollisionOccured : boolean = false;

function OnCollisionEnter(collision : Collision){

     if(CollisionOccured == false){

          PlatformCounter.Counter += 1;
          CollisionOccured = true;

     }
}

This should work unless your platforms can be collided by other objects than the player. If it’s the case, you need to add one more condition on the platform script, checking if the object that collide is the character.

Hi there thanks for that,

Yeah the platforms can, What would be the best condition to add in to check to see if its the character that is colliding with the platform. I have been trying other if statements and it is not working correctly, trying to even get it to just send a message to the log and I can’t even get that to work. Not winning on this script lol.

function OnCollisionEnter(collision : Collision)
{
     if(CollisionOccured == false)
     {

     PlatformCounter.Counter += 1;
     
     CollisionOccured = true;
     
     if(Collision.gameObject.tag("Player"))
     {
        Debug.Log ("Character landed on platform");
     }
    
   }    
     
}

Thank you for your help so far, it helps a lot :slight_smile:

My first thought was to have each platform hold the information on whether or not it has been stepped on, and have a central script that just counts have many have that value. If you have multiple players, you’d have to have them hold that information for each. Once a player steps on a platform, it sets it’s value for that player to true. That way a single platform can’t be counted more than once. The central script should just find all platforms at the start and keep a reference to them to make it easy to count up how many are set to true.

So, maybe try that version :

var CollisionOccured : boolean = false;
     
function OnCollisionEnter ( hit : Collision ){
    if(hit.gameObject.tag == "Player"){
       if(CollisionOccured == false){
         
       PlatformCounter.Counter += 1;
       CollisionOccured = true;
             
       }
           
     }    
}

I’ve tried in unity with that code and it’s working, make sure that at least the player or the platforms have a rigidbody and that they all have a collider component.

How about something like:

import System.Collections.Generic;

//var platformCounter;

var touched = new HashSet.<GameObject>();

function Update() {
    if (platformCounter.Counter >= 1000) {
        Application.LoadLevel(0);
        platformCounter.Counter = 0;
        touched.Clear();  // not necessary if this script will be destroyed by the scene change
    }
}
     
function OnControllerColliderHit(hit : ControllerColliderHit) {
    if (hit.gameObject.tag == ("platformCounter")  touched.Add(hit.gameObject)) {
        ++platformCounter.Counter;
        hit.collider.gameObject.active = true;  // how did this collide if the other GO wasn't active?
    }
}

Thanks for that, that works perfect, just need it to reset the score back to 0 now after level loads and I am sweet.

Thank you very much for all your help guys, absolute legends!

READ WHAT IT SAYS! JAVASCRIPT!!!

I’m guessing nobody here ever heard of JavaScript. It’s that language that Unity prevented from working in their later versions. It’s the language that come before C#.

Yes and no. JavaScript itself predates C# but the language that came with Unity isn’t JavaScript. It’s a bastardization known as UnityScript made to be compatible with .NET which meant that it was only somewhat compatible with JavaScript.

Unity’s C# on the other hand is truly C# meaning you can start working with it immediately whereas a JavaScript developer would have to learn the differences with UnityScript. Between that and UnityScript not receiving any real attention it’s not at all surprising that it eventually lost support.

That said it’s important to understand that it wasn’t the case that Unity removed it for no reason. Unity removed it because the overwhelming majority of developers simply stopped using it. Back in 2014 UnityScript had 20% usage but by the time they decided to remove it it was around 1 to 2% at most.

Keeping it would have been a drain on resources for virtually no advantage. If you can learn one language there is no reason you can’t learn a second language.

1 Like

It’s that language hardly anyone used with Unity anymore so became idiotic to dedicate half of Unity’s language maintenance resources towards. Yes we’ve heard of it.

You seriously necroposted a 5 year old post to yell and then act condescending?

2 Likes

Closed due to pointless necro and no longer relevant.