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.

In my example I am trying to show : C# creates an Instance of TP_Controller which TP_Motor can read from. My method loads TP_Motor into a var which through that var TP_Controller commands direct statements to TP_Motor . This works , but what if I want another script to read from TP_Controller ?

2 Answers

2

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.

This is awesome information , thankyou for taking the time to explain. My mind is slightly blown, I'm still playing with your first example using class and extending MonoBehaviour (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 Many Thanks :D

Just so you know, JS scripts are classes that extend MonoBehaviour by default. So you don't actually need the "public class MySingleton extends MonoBehaviour" 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 MonoBehaviour.

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 terminology 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.

Wow, this is pretty damn cool. I've been playing around with this this morning and I have some questions. What are the benefits of actually using GetComponents over static vars and the singleton pattern? I assumed from just some reading that either a) GetComponents were faster or b) Static variables couldn't be altered from other scripts ... but from a few tests I just did neither seem to be true ... Thanks by the way to @alucardj for linking this :)

@Kleptomaniac: static means there's only one instance in that class. It has nothing to do with access modifiers (public/private/protected etc.), and you still need to use an access modifier with a static variable, even if it's the implied modifier (private for C#, public for JS). It's not appropriate when you have multiple instances of something.

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

static var Instance : TP_Controller;

function Awake() {
    Instance = this;
}

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 MonoBehaviour. I appreciate you showing me the C# to JS conversion for this though, thanks again.