Search Unity

Javascript collision counter to go up by 1

Discussion in 'Scripting' started by DarkNeo, Apr 27, 2014.

Thread Status:
Not open for further replies.
  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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

    Code (csharp):
    1. static var Counter: int = 0;
    2.  
    3. function Update ()
    4. {
    5.  
    6.     guiText.text = "Platforms: "+Counter;
    7.    
    8.  
    9.     }
    Here is my script that is assigned to the character.

    Code (csharp):
    1. function Update(){
    2.     if(platformCounter.Counter >= 1000)
    3.     {
    4.        Application.LoadLevel(0);
    5.        platformCounter.Counter = 0;
    6.     }
    7. }
    8.  
    9. function OnControllerColliderHit(hit : ControllerColliderHit)
    10. {
    11.     if(hit.gameObject.tag == ("platformCounter"))
    12.     {
    13.        platformCounter.Counter = 1;
    14.        hit.collider.gameObject.active = true;
    15.        
    16.     }
    17. }
    18.  
    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..

    Code (csharp):
    1. platformCounter.Counter = 1;

    Cheers :)
     
  2. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    I think that you should try :

    Code (csharp):
    1. platformCounter.Counter += 1;
    or

    Code (csharp):
    1. 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.
     
    Last edited: Apr 27, 2014
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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
     
  4. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    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:

    Code (csharp):
    1.     static var Counter: int = 0;
    2.      
    3.     function Update ()
    4.     {
    5.      
    6.         guiText.text = "Platforms: "+Counter;
    7.         if(Counter >= 1000)
    8.     {
    9.  
    10.  
    11.        Application.LoadLevel(0);
    12.        Counter = 0;
    13.  
    14.     }
    15.        
    16.      
    17. }
    And a script on each platform, not on the character :
    Code (csharp):
    1. var CollisionOccured : boolean = false;
    2.  
    3. function OnCollisionEnter(collision : Collision){
    4.  
    5.      if(CollisionOccured == false){
    6.  
    7.           PlatformCounter.Counter += 1;
    8.           CollisionOccured = true;
    9.  
    10.      }
    11. }
    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.
     
    Last edited: Apr 27, 2014
  5. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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.

    Code (csharp):
    1. function OnCollisionEnter(collision : Collision)
    2. {
    3.      if(CollisionOccured == false)
    4.      {
    5.  
    6.      PlatformCounter.Counter += 1;
    7.      
    8.      CollisionOccured = true;
    9.      
    10.      if(Collision.gameObject.tag("Player"))
    11.      {
    12.         Debug.Log ("Character landed on platform");
    13.      }
    14.    
    15.    }    
    16.      
    17. }
    Thank you for your help so far, it helps a lot :)
     
  6. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    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.
     
  7. SmokyZebra

    SmokyZebra

    Joined:
    Mar 13, 2014
    Posts:
    139
    So, maybe try that version :

    Code (csharp):
    1.  
    2. var CollisionOccured : boolean = false;
    3.      
    4. function OnCollisionEnter ( hit : Collision ){
    5.     if(hit.gameObject.tag == "Player"){
    6.        if(CollisionOccured == false){
    7.          
    8.        PlatformCounter.Counter += 1;
    9.        CollisionOccured = true;
    10.              
    11.        }
    12.            
    13.      }    
    14. }
    15.  

    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.
     
    Last edited: Apr 27, 2014
  8. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    How about something like:

    Code (csharp):
    1.  
    2. import System.Collections.Generic;
    3.  
    4. //var platformCounter;
    5.  
    6. var touched = new HashSet.<GameObject>();
    7.  
    8. function Update() {
    9.     if (platformCounter.Counter >= 1000) {
    10.         Application.LoadLevel(0);
    11.         platformCounter.Counter = 0;
    12.         touched.Clear();  // not necessary if this script will be destroyed by the scene change
    13.     }
    14. }
    15.      
    16. function OnControllerColliderHit(hit : ControllerColliderHit) {
    17.     if (hit.gameObject.tag == ("platformCounter")  touched.Add(hit.gameObject)) {
    18.         ++platformCounter.Counter;
    19.         hit.collider.gameObject.active = true;  // how did this collide if the other GO wasn't active?
    20.     }
    21. }
    22.  
     
  9. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53

    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!
     
  10. RockingGameDeveloper

    RockingGameDeveloper

    Joined:
    May 28, 2015
    Posts:
    18
    READ WHAT IT SAYS! JAVASCRIPT!!!
     
  11. RockingGameDeveloper

    RockingGameDeveloper

    Joined:
    May 28, 2015
    Posts:
    18
    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#.
     
  12. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,193
    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.
     
    Last edited: Sep 20, 2019
    Joe-Censored likes this.
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You seriously necroposted a 5 year old post to yell and then act condescending?
     
    Joe-Censored and Ryiah like this.
  15. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Closed due to pointless necro and no longer relevant.
     
Thread Status:
Not open for further replies.