Unity: Have scripts run while the game doesn’t

There’s a way to force scripts to run even if the game is not running in the Editor:

In Javascript, simply put this on the top of the script:

@script ExecuteInEditMode()

And for C#:


Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.


    Mike_S on December 27, 2010 at 23:47 | Permalink


    1) How do you trigger the script?

    The only way I found so far was to drag it onto a GameObject in the Hierarchy window. To re-activate it,I used the "Remove Component" popup from the Inspector, then re-dragged the script from the Project list back onto the GameObject

    2)can you post a useful example ?


    col000r on December 28, 2010 at 10:29 | Permalink

    the script starts running as soon as it's done compiling, just put your code in Start or Update…

    I sometimes use this for building debug GUI with UnityGUI. Here's a simple script I use:

    using UnityEngine;
    using System.Collections;


    public class DEBUGDistanceBetween : MonoBehaviour {

    public Transform t1;
    public Transform t2;

    void OnGUI () {
    if(t1 && t2) GUI.Label(new Rect(20, 20, 200, 50), (t1.position – t2.position).magnitude.ToString("#.00"));

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>