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 /
avatar image
0
Question by Siachin · Apr 13, 2011 at 07:35 AM · editorsingletonbce0044bce0043

Singleton Error with code

Here is my MySingleton.js javascript with code.I am getting errors mentioned at bottom of code.Need a Score var to be updated in other class where i am accessing it.Please don't mind my stupidity in code as am a newbie....:)Thanks in advance.

public class MySingleton { private static MySingleton instance;

public MySingleton () { if (instance != null) { Debug.LogError ("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;
 }

}

private int score;

public int Score { get { return score; } set { score = value; } }

}

And the Error I am getting is:Assets/Test/MySingleton.js(3,20): BCE0043: Unexpected token: MySingleton.

Assets/Test/MySingleton.js(5,12): BCE0043: Unexpected token: MySingleton.

Assets/Test/MySingleton.js(5,24): BCE0044: expecting EOF, found '('.

Comment
Add comment · Show 4
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 Jake-L · Apr 13, 2011 at 07:46 AM 0
Share

I'm no javascript guy (so I may be wrong), but isn't there a "class" missing in your class declaration?

avatar image TheDemiurge · Apr 13, 2011 at 10:40 AM 0
Share

Not exactly an answer, but you may want to use C# for the singleton, if only because of this: http://answers.unity3d.com/questions/45879/can-i-declare-properties-in-js

avatar image Siachin · Apr 13, 2011 at 10:59 AM 0
Share

@Jake L:The line at the top of the box has the class declaration ,i don't know why was it not taken as part of code by the community server.

avatar image Siachin · Apr 13, 2011 at 11:02 AM 0
Share

@TheDemiurge:Ummm..i couldn't get that code.An answer in javascript would be helpful for the newbie.Still thanks for your effort.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jean-Fabre · Apr 18, 2011 at 10:28 AM

Hi,

Here is the full script you need to have:

make sure you create a c sharp script, cause this is csharp, not javascript. And make sure you name the script the same as the classe you define so your script file in your project must be named MySingleton.

using UnityEngine;

public class MySingleton {

 private static MySingleton instance;

 public MySingleton () 
 {
     if (instance != null)
     {
         Debug.LogError ("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;
     }
 }

   private int score;



 public int Score
 {
     get
     {
         return score;
     }
     set
     {
         score = value;
     }
 }

}

Bye,

Jean

Comment
Add comment · Show 3 · 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 Siachin · Apr 18, 2011 at 10:58 AM 0
Share

Thanks but Isn't the same code i have done in there??Will let you know when i try it out,Thanks by the way:)

avatar image Jean-Fabre · Apr 18, 2011 at 11:49 AM 0
Share

Yes, but you save it as a javascript, and you should create a c# script ins$$anonymous$$d and copy that content. It won't work as a javascript code.

avatar image Jean-Fabre · Apr 18, 2011 at 11:49 AM 0
Share

And also, I have added the include command ( first line ) otherwise you will get errors as well.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

error message for var 1 Answer

scripting colliders errors 1 Answer

Trying to Destroy Object when Wave starts 1 Answer

Null reference exception - singletone 0 Answers

How can solve these errors in my java script 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