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
private var startTime;
function OnTriggerStay(other : Collider)
{
if(other.gameObject.tag == "Player")
{
Debug.Log("Player OnTriggerStay");
startTime = Time.time;
if(startTime >= 4.0)
{
Debug.Log("Player destroyed GO");
ResetTimer();
Destroy(gameObject);
}
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == "Player")
{
Debug.Log("Player OnTriggerExit");
ResetTimer();
}
}
function ResetTimer()
{
startTime= 0.0;
}
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!)
Thanks, Joe.
I tried using float, and no errors, but it doesn’t appear to be working either.
My new script:
var startTime : float = 0;
function OnTriggerStay(other : Collider)
{
if(other.gameObject.tag == "Player")
{
Debug.Log("Player OnTriggerStay");
startTime = Time.time;
if(startTime >= 4.0)
{
Debug.Log("Player destroyed GO");
ResetTimer();
Destroy(gameObject);
}
}
}
function OnTriggerExit(other : Collider)
{
if(other.gameObject.tag == "Player")
{
Debug.Log("Player OnTriggerExit");
ResetTimer();
}
}
function ResetTimer()
{
startTime= 0.0;
}
Is decaling my variable with float the correct way to go?
- var startTime : float = 0;
I switched to C# and cobbled together something that works:
using UnityEngine;
using System.Collections;
public class timer : MonoBehaviour {
float t = 0;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag=="Beetle")
t = 0;
}
void OnTriggerStay(Collider other)
{
t += Time.deltaTime;
if(t > 3) {
Destroy(gameObject);
//Do something
}
}}
2 Likes
This link suddenly has a whole lot of relevance to this:
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.
Thanks for the tip- that’s an important consideration!
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;)
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)