Search Unity

Programming questions ...

Discussion in 'Scripting' started by DaveyJJ, May 26, 2005.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    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?
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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):
    1.  
    2. particleEmitter.emit = true;
    3. yield new WaitForSeconds (10);
    4. particleEmitter.emit = false;
    5.  
    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):
    1.  
    2. function OnMouseDown () {
    3.   renderer.material.color = Color.red;
    4. }
    5.  
    See the MonoBehaviour script reference for more supported event types.

    Yes. Simply write:

    Code (csharp):
    1.  
    2. // This simple script adds a force to the rigidbody every frame.
    3. function FixedUpdate () {
    4.    rigidbody.AddForce (Vector3.up);
    5. }
    6.  
    See the script reference on MonoBehaviour.

    Coroutines are perfect for this task.[/code]
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    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:
     
  4. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    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):
    1.  
    2. /*Get reference to the missile script.*/
    3. var missileComponent : missileScript = theMissile.GetComponent(missileScript);
    4.  
    5. /*Call the detonate() function after 4 seconds.*/
    6. theMissile.Invoke("detonate", 4);
    7.  
    Any tips are appreciated.
     
  5. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    You are calling Invoke on the GameObject. try calling it on missileComponent instead ;-)
     
  6. pdrummond

    pdrummond

    Joined:
    Jun 23, 2005
    Posts:
    185
    Just the ticket! Thanks.