x


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.AddComponent(KongApi);
Object.DontDestroyOnLoad (object);
Kong = object.GetComponent(KongApi);
Kong.Start();

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 ?

EDIT:

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 ?

more ▼

asked Jan 06 '11 at 08:35 PM

Steven 1 gravatar image

Steven 1
377 30 36 51

I doubt you should be calling Start yourself since the game will call Start.

Jan 06 '11 at 08:40 PM Statement ♦♦

yeah about that, I noticed it didn't call start, reason for that is that I defined the class in that script, apparently when you do that, the script then doesn't extends on Monobehaviour.

Jan 06 '11 at 08:52 PM Steven 1

Dont use object, use a different word...

Jan 06 '11 at 09:02 PM Justin Warner

you mean for the name of the gameobject instance ? yeah I guess that's bad choice of naming, but it shouldn't give any problems I think ? (it's case sensitive)

Jan 06 '11 at 09:36 PM Steven 1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

//KongAPI.js
static class KongAPI {

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

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

or:

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

           instance.DoSomething();
     }
}

//Some random script.js
function Awake () {
     KongAPI.InitKong(1);
}
more ▼

answered Jan 07 '11 at 02:44 AM

Peter G gravatar image

Peter G
15.1k 16 44 137

it was originally a singleton (atleast I think, it's been a couple of days, my memory is pretty bad :s). But for it to work on Kongregate, it has to be a GameObject. Ofcourse, I might just be able to inherit from GameObject, hm, why didn't I think of that sooner. Anyway, I already fixed it some other way back then (more like, avoided). But I'm leaving this open beacuse I'd still like to know why it doesn't work.

Jan 10 '11 at 09:00 PM Steven 1

oh wait, I read your post partly wrong. A static class ? I didn't even know you could that (or maybe I did, but forgot). (if previous comment wasn't clear: I mean I could have made that class inherit from gameobject and make it a singleton, instead of attaching the script to a gameobject)

Jan 10 '11 at 09:04 PM Steven 1

You shouldn't inherit from GameObject. Really the only classes you should inherit from are MonoBehavior, classes in the Mono library, some editor classes, a few Unity runtime classes located outside the Object class, or your own custom classes. Most of the Unity classes are either sealed or won't give you the results you expected.

Jan 10 '11 at 10:57 PM Peter G
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2171
x385
x312

asked: Jan 06 '11 at 08:35 PM

Seen: 1493 times

Last Updated: Jan 06 '11 at 08:58 PM