Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Scene script preprocessing

Discussion in 'Scripting' started by Richard_B, Aug 21, 2005.

  1. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Is is possible to have a script execute after all objects have loaded but before any Updates are called. The sort of thing I want to do is create a linked list of of a subset of the objects in the scene. Each object in this list can then scan the list, to, for example, get the positions (or other properties) of the other objects in the list.

    I guess I am asking what is the best way to set up some scene preprocessing.

    Thanks,
    Richard.
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Why not put that in the Start method of some game object?

    That's how I've always done it.

    Cheers,
    -Jon
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    There is Awake, Start and OnLevelLoad.

    Awake is called after everything else has been loaded (Meshes, game objects etc) on every script instance.
    (Only on active objects but it is called on enabled and disabled scripts)

    Start is called directly before the first function call to a script.
    This is useful for delayed activation. Like sleeping monsters.
    (Start is always called after all Awake functions)

    OnLevelLoad is called when a new level is loaded.
     
  4. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    What class are they derived from? I can't find OnLevelLoad in the docs.

    thanks,
    Richard.
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Isn't documented yet.
    Will fix that for 1.1.

    OnLevelLoad () isn't used very often though, only when you need to setup stuff that is very specific for one level.

    Awake and Start get the job done very well.
     
  6. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Is it part of the MonoBehaviour class - or some other class?

    thanks,
    Richard.
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    OnLevelLoad is a function you can overload if you derive from MonoBehaviour. Same with Start and Awake
     
  8. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    If the script is derived from MonoBehaviour then it has to be attached to an object right?

    While this is OK (I can just have a dummy transform object), I was just wondering if any scripts (such as the OnLevelLoad) are executed automatically without being associated with any object?

    thanks,
    Richard.
     
  9. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    What I have done is create some blank game object in your scene and call it like Controller or something and put all those types of scripts there.

    -Jon
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    No script functions are ever called if they are not attached to game object.

    We are planning to introduce static functions and static editable properties to alleviate this, in a later release.
     
  11. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Ok - no problem - I'll just use a dummy object,
    thanks for the help,
    Richard.