Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 22, 2013 at 08:41 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by Friso · May 22, 2013 at 04:43 AM · sceneinstancekinectsingletonwrapper

How do I stop a singleton from trying to load itself again when I change scenes?

I am working on a Kinect project and I use a wrapper (this one: http://wiki.etc.cmu.edu/unity3d/index.php/Microsoft_Kinect_-_Microsoft_SDK ).

There can be only one instance of the Kinect sensor, but it tries to make a new one when I try to load a different scene. This causes it not to work anymore. The problem stems from these lines of code:

     private static KinectInterface instance;
 public static KinectInterface Instance
 {
     get
     {
         if (instance == null)
             throw new Exception("There needs to be an active instance of the KinectSensor component.");
         return instance;
     }
     private set
     { instance = value; }
 }

And the Awake() function:

 void Awake()
 {
     if (KinectSensor.instance != null)
     {
         Debug.Log("There should be only one active instance of the KinectSensor component at at time.");
     throw new Exception("There should be only one active instance of the KinectSensor component at a time.");
     }
     try
     {
         // The MSR Kinect DLL (native code) is going to load into the Unity process and stay resident even between debug runs of the game.  
     // So our component must be resilient to starting up on a second run when the Kinect DLL is already loaded and
     // perhaps even left in a running state.  Kinect does not appear to like having NuiInitialize called when it is already initialized as
     // it messes up the internal state and stops functioning.  It is resilient to having Shutdown called right before initializing even if it
     // hasn't been initialized yet.  So calling this first puts us in a good state on a first or second run.
         // However, calling NuiShutdown before starting prevents the image streams from being read, so if you want to use image data
         // (either depth or RGB), comment this line out.
         //NuiShutdown();
         
         int hr = NativeMethods.NuiInitialize(NuiInitializeFlags.UsesDepthAndPlayerIndex | NuiInitializeFlags.UsesSkeleton | NuiInitializeFlags.UsesColor);
     if (hr != 0)
         {
         throw new Exception("NuiInitialize Failed.");
         }
         
         hr = NativeMethods.NuiSkeletonTrackingEnable(IntPtr.Zero,0);
         if (hr != 0)
         {
             throw new Exception("Cannot initialize Skeleton Data.");
         }
         
         depthStreamHandle = IntPtr.Zero;
         hr = NativeMethods.NuiImageStreamOpen(NuiImageType.DepthAndPlayerIndex, NuiImageResolution.resolution320x240, 0, 2, IntPtr.Zero, ref depthStreamHandle);
         Debug.Log(depthStreamHandle);
         if (hr != 0)
         {
             throw new Exception("Cannot open depth stream.");
         }
         
         colorStreamHandle = IntPtr.Zero;
         hr = NativeMethods.NuiImageStreamOpen(NuiImageType.Color, NuiImageResolution.resolution640x480, 0, 2, IntPtr.Zero, ref colorStreamHandle);
         Debug.Log(colorStreamHandle);
         if (hr != 0)
         {
             throw new Exception("Cannot open color stream.");
         }
         colorImage = new Color32[640*480];
         
         double theta = Math.Atan((lookAt.y+kinectCenter.y-sensorHeight) / (lookAt.z + kinectCenter.z));
         long kinectAngle = (long)(theta * (180 / Math.PI));
         NativeMethods.NuiCameraSetAngle(kinectAngle);
         
         DontDestroyOnLoad(gameObject);
         KinectSensor.Instance = this;
     }
     catch (Exception e)
     {
         Debug.Log(e.Message);
     }
     enabled = true;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Friso · May 22, 2013 at 08:41 AM

I've solved it, albeit in a bit crude way.

I've changed the following lines from Awake():

 if (KinectSensor.instance != null)
     {
        Debug.Log("There should be only one active instance of the KinectSensor component at at time.");
     throw new Exception("There should be only one active instance of the KinectSensor component at a time.");
     }

into:

 if (KinectSensor.instance != null)
     {
        Debug.Log("There should be only one active instance of the KinectSensor component at at time.");
     KinectSensor.instance = null;
     }

This makes the instance empty and allows it to be loaded again.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mystery_planet · Mar 22, 2014 at 02:33 AM 0
Share

YOU ROC$$anonymous$$!!!!!

avatar image kockaart · Mar 31, 2014 at 08:37 PM 0
Share

you're the $$anonymous$$AN!!!!!!!!!!

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnDisable calls singleton instance, creates unwanted object + errors 1 Answer

Singleton without MonoBehaviour? 1 Answer

Singleton instance accessed in coroutine always null 1 Answer

How to subtract variable from singleton instance? 0 Answers

What for are PlayerPrefs with 2 or more Scenes? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges