problem using Object.DontDestroyOnLoad()

i'm making a game to put on kongregate, in order to use their API, I have to send a function for it to call when the API is initialized. For some reason, the script this function is in has to be attached to a gameobject (so that the function's object is a gameobject).

(it really annoys me that for a lot of things, unity developers are being forced to use gameobjects for no good reason)

to do this I did this:

//Kong = new KongApi(); //nope, t moet weer aan een gameobject hangen
var object : GameObject;
object = new GameObject("MyUnityObject");
Object.DontDestroyOnLoad (object);
Kong = object.GetComponent(KongApi);

now this gives me an error for some reason, it claims DontDestroyOnLoad() doesn't exist. (I wrote Object. because this code isn't run in a script attached to a gameobject) when I remove that line, it compiles, but then I get a NullReferenceException: Object reference not set to an instance of an object for the Kong object, which means the GetComponent(KongApi) failed.

Anyone know what I'm doing wrong ?


it seems that scripts don't extend on Monobehaviour when you define the class in it ("class KongApi"), so because of this, it couldn't add it as a component. (that should be better documented IMO, though it actually does make sense). So that was fixed when I wrote it myself ("class KongApi extends MonoBehaviour").

but then there is still the problem that it doesn't seem to know DontDestroyOnLoad()

why is that ?

I could be completely wrong about your intentions, and if so I apologize, but why don't you just make it a static class. I don't know how the KongAPI works, but it sounds like you either want a Singleton instance or just to make it a static class just to avoid game objects all together and your actions should carry over between scenes (at least your singleton will)

Then you won't have to add it to a game object (I think your making it sound like more trouble than it is), and just call it from another script.

static class KongAPI {

      public static function InitKong(args) {
           //Initialize Kong API.

      public static function Magic () {
           //The magic happens here :)


class KongAPI {
     static instance : KongAPI;

     function getInstance() : KongAPI {
           if (instance == null) {
                instance = new KongAPI();
           return instance;

     function InitKong (args) {
           if(instance == null)
                getInstance(); //Using it to create the singleton


//Some random script.js
function Awake () {