Jump to content

Keeping MonoBehavior alive during game scene switch?


Recommended Posts

I'm not certain but you may also need to make sure that another one doesn't get created every time you switch. I think the "once" parameter on KSPAddon is meant for this purpose. I suggest adding some logging to your constructor, OnStart and OnAwake to make sure you're not getting lots of them...

Link to comment
Share on other sites

  • 7 years later...
On 7/20/2015 at 12:28 PM, *Aqua* said:

I noticed a MonoBehavior gets destroyed when the game scene is switched.

Is there a simple way (a flag maybe?) to keep it alive until the game shuts down?

The MonoBehaviour is always destroyed at the end of a Scene when decorated with a KSPAddon decorator.

To prevent it from being destroyed, you need to Instance yours manually (sometimes it can be a good idea) or, better yet, do the following call on the Awake() callback:

internal void Awake()
{
	DontDestroyOnLoad(this.gameObject);
	// The rest of your code, if existent
}

You NEED to set the once parameter of the KSPAddon decotator to true, or you will have multiple instances of that MonoBehavirour in memory as the scenes changes!

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...