Unity3D JavaScript - Timer Problem

Hi everyone,

I started with Unity3D 4-5 days ago and i used Google pretty well :slight_smile:

Now i came out to one script trouble i cant fix…

First i have used this script on other object and it worked but now i cant make it work for some reason…

var timer : float;
var starttime : boolean = false;
static var page1 : boolean = false;
static var pagescount = 0;

function OnMouseUp () 
{
	if(starttime == false)
	{
	gameObject.Find("Page_1").active = false;
	page1 = true;
	pagescount += 1;
	gameObject.Find("GUI_Pages").guiText.text = pagescount + " / 8";
	starttime = true;
	gameObject.Find("GUI_Pages_Mid").guiText.text = "Page collected " + pagescount + "/8";
	}
}

function FixedUpdate()
{
	if (timer >= 2)
	{
		starttime = false;
		timer = 0;
		gameObject.Find("GUI_Pages_Mid").guiText.text = " ";
	}
	if (starttime == true)
	{
		timer += 1.0F * Time.deltaTime;
	}
}

function OnMouseEnter()
{
	gameObject.Find("GUI_Text").guiText.text = "Pick up the page...";	
}

function OnMouseExit()
{
	gameObject.Find("GUI_Text").guiText.text = "Nothing here ....";
}

I script on Java Script, i used float timer multiple times but its always the var and not static var…

I am not sure why is this doesn’t work

I want to display something on GUI_Pages_Mid (GUI text) and then after some time i want it to disappears by type " " into the GUI_Pages_Mid …

I know that i do something wrong but can someone help :?

Ignore my bad English…

Fistly, I am disgusted with the use of GUI Text as actual GUI! (dw rookie mistake)
Please refrain from using it for such purpose. Instead use GUI functions inside the OnGUI runtime loop.

Another problem, is that your using Time.deltaTime in FixedUpdate with a timer. Time.deltaTime is the magic time (in seconds) between this and the last frame rendered, while FixedUpdate is called 50 times a second, independent from framerate.

Your general use of a timer is wrong as well. Having a timer with the current ingame time is terribly inefficient. Instead use Time.time, which gives you the current runtime in seconds, and save the point in time, where you want to stop.

var timeOut:float = 2;
private var timer:float;

//This is called when the object is made/scene is loaded
function Awake() {
    timer = Time.time + timeOut;
}

//Frame Update function
//This is with your current GUI Method
function Update() {
    //check if timer has run out
    if (timer < Time.time) {
        //set you GUI to " " and other stuff here
        gameObject.Find("GUI_Pages_Mid").guiText.text = " ";
    }
}

//This is the proper GUI Method!!!
function OnGUI() {
    if (timer < Time.time) {
        GUILayout.Label("text");
    }
    else {
        GUILayout.Label("");
    }
}

Hope this helps,
Benproductions1

Ohhh my god…

I have fixed the trouble…

Because i attached script to Page 1 and then make Page1.active = false; script is lost…

Anyway i found another way of doing same thing but much easier.

function OnMouseUp()
{
	if(Page1)
	{
		PageCount += 1;
		gameObject.Find("GUI_Pages_Mid").guiText.text = Text1 + PageCount + Text2;
		gameObject.Find("Page_1").renderer.enabled = false;
		yield WaitForSeconds(3);
		gameObject.Find("GUI_Pages_Mid").guiText.text = " ";
	}
}

I have changed .active to .renderer.enabled and big thing and life saver is yield WaitForSeconds command.

Now when i know this it will hopefully be much easier for me to do stuff in Unity…
Thou thanks for answers i appreciate it very much!