Just a quick couple of thrown out questions: 1. Can you register and program timers? 2. Can you create call back functions that you can register against an event, i.e., a timer? 3. Can you register a call back against a render frame event? Example: every time the engine calls a frame you can update physics, physics, position of an object? 4. Is there a list of all of the events that the Unity engine generates for use in scripting? 5. Can you add threads to run concurrent tasks; e.g, time thread?
Yes there are two ways to do this. Coroutines and Invoke. The recommended way is using coroutines. Coroutines are functions which can be paused at any point using the yield statement. for example you could have a Javascript with these 3 lines: Code (csharp): particleEmitter.emit = true; yield new WaitForSeconds (10); particleEmitter.emit = false; The script will enable particle emission. Then wait for 10 seconds. When the script gets control back it will start emission again. See the script reference on MonoBehaviour. You just write a function with the same name as the event. This script will change the color of the material when the mouse is over the mesh. Code (csharp): function OnMouseDown () { renderer.material.color = Color.red; } See the MonoBehaviour script reference for more supported event types. Yes. Simply write: Code (csharp): // This simple script adds a force to the rigidbody every frame. function FixedUpdate () { rigidbody.AddForce (Vector3.up); } See the script reference on MonoBehaviour. Coroutines are perfect for this task.[/code]
Just in addition to Joe - there is a more old-school way of getting a callback in,say, 10 seconds without changing your program flow... MonoBehaviour.Invoke ("Function name", time) friends (CancelInvoke, IsInvoking) and the _really_ nice one: coroutines.... There's one example in the MonoBehaviour scripting docs (this has not been converted to JavaScript yet, but should be the same... I'll try to get that done this week:
How can I invoke a function in another object? My player is able to fire missiles which I want to automatically destroy after a few seconds if they haven't hit anything by calling the detonate() function. I already have a reference to the script the missile object uses so I can set variables, but trying something like the following gives me an ''Invoke' is not a member of 'UnityEngine.GameObject'' error. Code (csharp): /*Get reference to the missile script.*/ var missileComponent : missileScript = theMissile.GetComponent(missileScript); /*Call the detonate() function after 4 seconds.*/ theMissile.Invoke("detonate", 4); Any tips are appreciated.