• 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 /
avatar image
0
Question by Alienjesus · Apr 27, 2011 at 05:15 PM · monobehaviourexceptionsingletonargumentexceptioncheckpoint

Checkpoint script problems - C#

I'm trying to write a checkpoint script, which requires use of a singleton, forcing me to work in C# instead of javascript. I know next to nothing about coding in C#, and the Unity documentation for it is frankly awful, so I'm having a hell of a time getting anything to work.

What I want to do is to save a float called checkpoint into the singleton, which can then be accessed and changed by outside javascript scripts. However, I can't get any of it working. Here's the C# code I currently have:

using UnityEngine; public class MySingleton: MonoBehaviour { private static MySingleton instance;

 public MySingleton () 
 {
     if (instance != null)
     {
         //Debug.Log("Cannot have two instances of singleton. Self destruction in 3...");
         return;
     }

     instance = this;
 }

 public static MySingleton Instance
 {
     get
     {
         if (instance == null)
         {
             new MySingleton ();
         }

         return instance;
     }
 }
 public float checkpoint = 0;
 void Update(){
 Debug.Log(checkpoint);
 }

}

However, when it's I die and the level resets, I get the following error:

ArgumentException: CompareBaseObjects  can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

If i remove the : Monobehaviour from the 2nd line, I don't get that error, but the Debug.Log line fails to work. No matter which way I have it though, on resetting the level, checkpoint resets to 0, defeating the point of the thing in the first place.

Please help, it's only a few days to my deadline, and I wasn't expecting to have to learn C# all of a sudden >_<.

Thanks

Comment
Add comment · Show 1
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 flaviusxvii · Apr 27, 2011 at 09:22 PM 0
Share

$$anonymous$$ake sure to mark answers as answers and vote if you think comments and answers are helpful.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by flaviusxvii · Apr 27, 2011 at 07:30 PM

"which requires use of a singleton, forcing me to work in C# instead of javascript"

Where did you get that idea? You can make singletons in Javascript.

Something like this:

private static var Instance : MySingletonClass = new MySingletonClass();

public static function GetInstance() : MySingletonClass { return Instance; }

Comment
Add comment · Show 4 · 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 Alienjesus · Apr 27, 2011 at 07:47 PM 0
Share

God knows, I know I read it somewhere though. You're a lifesaver, I've been looking all over the place for a java solution for singletons :D

I'm still confused as to how they work though. How do I use this to save my checkpoint data? I just tried and it still skips back to 0 when the level resets :S

avatar image flaviusxvii · Apr 27, 2011 at 08:20 PM 0
Share

Remember not to confuse "Java" and "Javascript". They have unfortunately similar names but are totally unrelated.

"when the level resets" - Does that mean when you switch scenes?

avatar image Alienjesus · Apr 27, 2011 at 08:45 PM 0
Share

It means when I reload the same scene. When I die it reloads the current scene (because I need to return many objects to their original positions and it was too memeory intensive to set them all back one by one in code). I want the singleton to save the last checkpoint hit so that i don't always restart from the beginning of the stage when I die.

avatar image flaviusxvii · Apr 27, 2011 at 09:20 PM 1
Share

Asked & Answered ;D http://answers.unity3d.com/questions/8387/avoid-nullreferenceexception-with-singleton

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How can I return a GameObject? 1 Answer

App Crash IE8 - UnityScriptCompiler 1 Answer

System.ArgumentException: FindObjectsOfType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. 0 Answers

How do I use a game object to store a singleton? 1 Answer

What's the proper way to see if a Vector3 List index is null? 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