Search Unity

(SOLVED) OnTriggerStay using Time.time (What did I do wrong?)

Discussion in 'Scripting' started by SiMULOiD, Aug 2, 2015.

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

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    This Javascript looks like it should work, but getting the error:

    js(11,22): BCE0051: Operator '>=' cannot be used with a left hand side of type 'Object' and a right hand side of type 'float'.

    Any thoughts?
    Thanks,
    Greg

    Code (csharp):
    1.  
    2.  
    3.  
    4. private var startTime;
    5.  
    6. function OnTriggerStay(other : Collider)
    7. {
    8.    if(other.gameObject.tag == "Player")
    9.        {
    10.          Debug.Log("Player OnTriggerStay");
    11.          startTime = Time.time;
    12.        
    13.         if(startTime >= 4.0)
    14.           {
    15.             Debug.Log("Player destroyed GO");
    16.             ResetTimer();
    17.             Destroy(gameObject);
    18.           }
    19.        }
    20. }
    21.  
    22. function OnTriggerExit(other : Collider)
    23. {
    24.    if(other.gameObject.tag == "Player")
    25.      {
    26.       Debug.Log("Player OnTriggerExit");
    27.       ResetTimer();
    28.      }
    29. }
    30.  
    31. function ResetTimer()
    32. {
    33.    startTime= 0.0;
    34. }
    35.  
    36.  
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'm not sure (I don't use JavaScript), but try declaring startTime as 'float' instead of 'var'.

    (And if this works, the general lesson is that it's almost always better to specifically type your properties; avoid var in most situations!)
     
  3. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks, Joe.
    I tried using float, and no errors, but it doesn't appear to be working either.
    My new script:

    Code (csharp):
    1.  
    2. var startTime : float = 0;
    3.  
    4.  function OnTriggerStay(other : Collider)
    5.  {
    6.    if(other.gameObject.tag == "Player")
    7.        {
    8.          Debug.Log("Player OnTriggerStay");
    9.          startTime = Time.time;
    10.          
    11.         if(startTime >= 4.0)
    12.           {
    13.             Debug.Log("Player destroyed GO");
    14.             ResetTimer();
    15.             Destroy(gameObject);
    16.           }
    17.        }
    18.  }
    19.  
    20.  function OnTriggerExit(other : Collider)
    21.  {
    22.    if(other.gameObject.tag == "Player")
    23.      {
    24.       Debug.Log("Player OnTriggerExit");
    25.       ResetTimer();
    26.      }
    27.  }
    28.  
    29.  function ResetTimer()
    30.  {
    31.    startTime= 0.0;
    32.  }
    33.  
     
  4. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Is decaling my variable with float the correct way to go?
    1. var startTime : float = 0;

     
  5. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    I switched to C# and cobbled together something that works:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class timer : MonoBehaviour {
    6.  
    7.    float  t = 0;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         if(other.gameObject.tag=="Beetle")
    12.        
    13.        t = 0;
    14.     }
    15.  
    16.     void OnTriggerStay(Collider other)
    17.     {
    18.          t += Time.deltaTime;
    19.          if(t > 3) {
    20.            
    21.             Destroy(gameObject);
    22.             //Do something
    23.          }
    24.     }}
    25.  
     
    DonLoquacious and JoeStrout like this.
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    This link suddenly has a whole lot of relevance to this:
    http://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html

    Be careful about using both Javascript and C# in your projects, unless the portions that use one and the portions that use the other are completely self-contained and don't need to interact with one another.
     
  7. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thanks for the tip- that's an important consideration!
     
  8. Omnindian

    Omnindian

    Joined:
    Jun 1, 2020
    Posts:
    7
    I know this is an old thread but it seems that neither of your first two (javascript codes) has any way to increment time but I'm not versed in this language.
    You c# code does (t += Time.deltaTime;)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    I'm pleased that you know it's an old thread but please also understand that necroing threads like this on an obsolete subject is against the forum rules. It just adds extra unwanted noise to a an already noisy forum.

    1i. Pointless necroposting (posting in a forum thread that is too old to matter any more, or has served its purpose)
     
Thread Status:
Not open for further replies.