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 androids · Nov 27, 2013 at 02:54 PM · c#javascripttranslate

Translate C# into Js

Hi folks,

I got this C#, but I have no experience with C#. Is there someone who can translate this C# script into JavaScript?

 using UnityEngine;
 using System.Collections;
 
 public class MyUnitySingleton : MonoBehaviour {
  
 private static MyUnitySingleton instance = null;
 public static MyUnitySingleton Instance
 {
     get
     {
         return instance;
     }
 }
 
 void Awake()
 {
     if (instance != null && instance != this)
     {
         Destroy(this.gameObject);
         return;
     }
     else
     {
         instance = this;
     }
     
     DontDestroyOnLoad(this.gameObject);
     }
 }


Thanks in advance.

Comment
Add comment · Show 3
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 ArkaneX · Nov 27, 2013 at 03:10 PM 0
Share

Have you at least tried converting it? This script is fairly easy to convert, even without C# knowledge. I will gladly help, if you write which part of this script you have problem with.

avatar image androids · Nov 27, 2013 at 03:23 PM 0
Share

Okay Let me try to translate it.

     private var $$anonymous$$yUnitySingleton instance = null;
     public var $$anonymous$$yUnitySingleton Instance;
     {
         get
         {
            return instance;
         }
     }
      
     function Awake()
     {
         if (instance != null && instance != this)
         {
            Destroy(this.gameObject);
            return;
         }
         else
         {
            instance = this;
         }
      
         DontDestroyOnLoad(this.gameObject);
     }

This is so far I could translate it, but it's totaly wrong.

avatar image MrVerdoux · Nov 27, 2013 at 04:47 PM 0
Share

There´s another solution: forget javascript and learn the joy of C# :D

1 Reply

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

Answer by ArkaneX · Nov 27, 2013 at 03:43 PM

There are two errors in your conversion:

1) you declare variables in UnityScript differently:

 private var instance : MyUnitySingleton  = null;

2) getter in US is declared in this way:

 public function get Instance() : MyUnitySingleton { return instance; }

Awake is ok.

Comment
Add comment · Show 6 · 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 androids · Nov 27, 2013 at 04:09 PM 0
Share

Thanks, It works perfect. If I leave the scene, the music is still playing. But if I return to that scene where the script is. The sound is playing again. While the song is already playing. That means you hear the same sound twice ate one time. How to solve this problem?

avatar image ArkaneX · Nov 27, 2013 at 04:39 PM 0
Share

It's hard to answer this question without knowing how you're using this script. But I think you should post it as a separate question, as this problem is unrelated to the original one. This way more users will be able to help.

avatar image clunk47 · Nov 27, 2013 at 07:27 PM 0
Share

Sounds like your gameObject is an AudioSource. Your music is running over itself because when you don't destroy a gameObject, and reload the scene, the original gameObject is left in the scene along with your new one.

avatar image ArkaneX · Nov 27, 2013 at 07:52 PM 0
Share

Shouldn't this new one be destroyed in Awake? Condition of first if should eval to true.

avatar image androids · Dec 03, 2013 at 12:06 AM 0
Share

I tried a lot, but the problem is still unsolved. It plays all over again when I return to the scene with that $$anonymous$$usicObject. The $$anonymous$$usic plays over the same $$anonymous$$usic.

Help please

Show more comments

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

19 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 avatar image avatar image avatar image avatar image

Related Questions

How to make this line work in C#? 1 Answer

C# to java translation 2 Answers

About "translating" js into C# 1 Answer

Translate Unity-scripts to uScript 1 Answer

java to C# conversion 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