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
1
Question by AlucardJay · Apr 04, 2012 at 06:15 PM · c#javascriptstaticinstance

how do I create a static Instance in javascript

How do I create an 'Instance' in JavaScript for my other scripts to reference ?

C# example :

 using UnityEngine;
 using System.Collections;
 
 public class TP_Controller : MonoBehaviour 
 {   
     public static TP_Controller Instance;
 
     void Awake() {
         Instance = this;
     }
 }

My js workaround for where the C# script creates an 'Instance' to read and write vars from , I found the other scripts and stored them in local vars.:

 public var TP_Motor : Script_TP_Motor;
 function Awake() {
     TP_Motor = GetComponent("Script_TP_Motor") as Script_TP_Motor;
 }

Any help or info would be greatly appreciated.

This is the same as my other question , but it was answered with (helpful) information not specific to my question. http://answers.unity3d.com/questions/235181/help-with-creating-a-class-in-javascript.html

I shall remove the duplicate when I hopefully have an answer to help clean up the system. 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 AlucardJay · Apr 04, 2012 at 06:45 PM 0
Share

In my example I am trying to show :

C# creates an Instance of TP_Controller which TP_$$anonymous$$otor can read from.

$$anonymous$$y method loads TP_$$anonymous$$otor into a var which through that var TP_Controller commands direct statements to TP_$$anonymous$$otor .

This works , but what if I want another script to read from TP_Controller ?

2 Replies

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

Answer by by0log1c · Apr 04, 2012 at 06:23 PM

Just to clarify, static class and instances are opposite concepts. From the look of things, I'd say you're interested in the Singleton pattern.

EDIT: Well you could either have a static class or a regular class with static variables. Here's a quick example:

 //MyScript.js
 public class MyScript extends MonoBehaviour
 {
     public static var myName:String = "By0log1c";
     public var myLocation:String = "Quebec, Canada";
 
 }
 
 //MyStaticScript.js
 //static class must extend from object (just leave it blank)
 public static class MyStaticScript
 {
     //myAge is static since its within MyStaticScript
     public var myAge:int = 23;
 }
 
 //SomeScript.js
 public class SomeScript extends MonoBehaviour
 {
     function Start():void
     {
         Debug.Log(MyScript.myName); //MyScript.myName is static
         Debug.Log(MyStaticScript.myName); //MyStaticScript is static
     
         Debug.Log(MyScript.myLocation); //nothing static here = error!
     }
 
 }

Frankly, the singleton pattern is what really taught me what static is. Here's a JS example:

 //let's combine the best of both world!
 public class MySingleton extends MonoBehaviour
 {
     private static var instance:MySingleton;
     public static function Instance():MySingleton
     {
         return instance;
     }

     var nonStaticVariable:boolean = false;
 
     function Awake():void
     {
         instance = this;
     }
 }

 //SomeScript.js
 public class SomeScript extends MonoBehaviour
 {
     function Start():Void
     {
         //we access MySingleton through its static Instance
         //function and use a MonoBehaviour like it was static!
         Debug.Log(MySingleton.Instance().nonStaticVariable);
     }
 }

That's a lot of reading but trust me, you'll love singleton.

Comment
Add comment · Show 16 · 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 AlucardJay · Apr 04, 2012 at 06:37 PM 0
Share

I am having trouble expressing the problem. I want to write in JavaScript (not knowing any C# and limited js, looking at your Singleton link it's in C#?).

The only way I can make an analogy is with my previous experience in actionscript2.

1/ in frame 1 I have 'health = 10;'

2/ in frame 2 I have an empty $$anonymous$$ovieClip with the action '_root.health -= 5;'

health is just a var that is created and exists somewhere special. And anytime I want to read or write to health, I call it with _root.

So in js, I want to create an 'Instance' my _root. master read/write/store data place. So when I want to have multiple scripts access data, they can call scriptInstance.foo ins$$anonymous$$d of me loading all the other scripts in each script with GetComponent.

$$anonymous$$y understanding of static was calling static on a var would then only allow one Instance/var be in existance. But of course I could be wrong.

avatar image AlucardJay · Apr 06, 2012 at 01:56 AM 0
Share

This is awesome information , thankyou for taking the time to explain. $$anonymous$$y $$anonymous$$d is slightly blown, I'm still playing with your first example using class and extending $$anonymous$$onoBehaviour (something else I didn't understand from following the 3D Buzz TPC tutorial). I have lots to pick up and absorb before playing with Singletons , but I shall definitely check them out too. for now $$anonymous$$any Thanks :D

avatar image Eric5h5 · Apr 06, 2012 at 02:10 AM 1
Share

Just so you know, JS scripts are classes that extend $$anonymous$$onoBehaviour by default. So you don't actually need the "public class $$anonymous$$ySingleton extends $$anonymous$$onoBehaviour" stuff, you can just leave it out and it will work the same. You only need to define the class if you don't want to extend from $$anonymous$$onoBehaviour.

avatar image AlucardJay · Apr 06, 2012 at 07:31 AM 0
Share

Thankyou both @BY0LOG1C and @Eric5h5 , the information I have gained and things I have learned from your comments have been amazing. Being self-taught from forums, sometimes the ter$$anonymous$$ology is lost on me. Yes, google is my friend, but sometimes it's nice to have a breakdown or an example so I can see what's happening.

avatar image by0log1c · Apr 11, 2012 at 06:35 AM 1
Share

The whole idea of the SINGLEton pattern is to have only one object of said Type or the last object to fill the instance variable will be your singleton, making every other object just regular instance. There's lots of room to add your own logic on top (hmm, a Dictionary?), but as a general guideline, its not very appropriated. As much as I love singletons, I only really use them in case where they actually make sense :D

Show more comments
avatar image
3

Answer by Eric5h5 · Apr 04, 2012 at 06:45 PM

The JS equivalent of the C# code you posted is:

 static var Instance : TP_Controller;

 function Awake() {
     Instance = this;
 }
Comment
Add comment · Show 1 · 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 AlucardJay · Apr 04, 2012 at 06:52 PM 0
Share

Thankyou , but @BY0LOG1C answer helps me understand what I'm trying to do (as I only half-knew what I wanted!), as well as introducing class and extending $$anonymous$$onoBehaviour. I appreciate you showing me the C# to JS conversion for this though, thanks again.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How do i reference a non static singleton? 2 Answers

How do i reference a non static singleton? 1 Answer

Distribute terrain in zones 3 Answers

Can't access a javascript static variable from c# 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