help with creating a static Instance in javascript

// – Amended Question –

I have successfully rewritten a Third Person Controller tutorial on 3D Buzz from C# to JavaScript, with one major exception. Where the C# script creates an ‘Instance’ to read and write vars from , I found the other scripts and stored them in local vars. This is going to give me future problems so I would like to finally find out :

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 :

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

Any help or info would be greatly appreciated.

// Thanks , Jay.

//

// ---- First Posted Question ----

I am following the Third Person Controller videos on 3D Buzz, but they are written in C#. My goal is to follow the video’s and write the code in JavaScript.

From what I understand that I am trying to do, is establish a set of vars to live in a ‘class’ , then I should create an instance of this class, for other scripts to access vars or adjust vars.

In my previous projects, to read/edit other scripts’ data I have simply assigned a e.g. var myTPC : ScriptTPC; and used GetComponent in the Start to load the script.

So before I start; can someone please help me understand what is being set up in this C# script :

using UnityEngine;
using System.Collections;

public class TP_Controller : MonoBehaviour 
{	
	public static CharacterController myCharacterController;
	public static TP_Controller Instance;

	void Awake() 
	{
		myCharacterController = GetComponent("CharacterController") as CharacterController;
		Instance = this;
	}
}

and how can I edit my converted javascript to use class and assign Instance :

#pragma strict

public static var myCharacterController : CharacterController;
public static var TP_Controller : Instance;

function Awake() 
{
	myCharacterController = GetComponent("CharacterController");
	Instance = this;
}

Many thanks, I am self-taught so alot of the terminology is lost on me.

3D Buzz link : http://www.3dbuzz.com/vbforum/content.php?212

Scroll down to → Section 2 ; Video 6 - TP_Controller Skeleton

Edit 1 : I understand I have to load CharacterController , and this actually loads preset functions I can call on (TransformDirection , Move, IsGrounded etc) , I don’t understand how to create an ‘Instance’ that all scripts can call on. My method would be to load the other scripts into a var then edit e.g. var myTPC : ScriptTPC; myTPC.posX = 1; (would change the posX var on ScriptTPC to 1). Can I just do this instead of creating an Instance?

Edit 2 : So far my scripts are working , using a different method of reading and writing to each other. Although I do get a Warning : BCW0028: WARNING: Implicit downcast :

public var myCharacterController : CharacterController;
// public static var TP_Controller : Instance;
public var TP_Motor : Script_TP_Motor;

function Awake() 
{
	myCharacterController = GetComponent("CharacterController");
	// Instance = this;
	TP_Motor = GetComponent("Script_TP_Motor"); // Warning , even with using -> OR transform.gameObject.GetCo... OR this.GetCo...
}

and I’ll have to do the same for the TP_Camera script

Answered on this Question :

http://answers.unity3d.com/questions/235417/how-do-i-create-a-static-instance-in-javascript.html

using UnityEngine;
using System.Collections;

public class TP_Controller : MonoBehaviour 
{   
    public static CharacterController myCharacterController;       //sets a variable of type CharacterController
    public static TP_Controller Instance;                                      //sets a variable of type TP_Controller (this script)

    //Happens only once when the script is first created
    void Awake()
    {
       //Assigns the CharacterController component to a varaiable so it can be called by this script
       //the Get Component function will look for the component on the object to which this script is attached
       myCharacterController = GetComponent("CharacterController") as CharacterController; 

       //set this script to a variable.  I'm not sure why you would want to do this, it doesn't make any sense
       Instance = this;
    }

   //code in the Update method happens every frame
    void Update()
    {
       //if there is no camara then skip the update
       if (Camera.mainCamera == null)
         return;

       //calls the GetLocomotionInput method (see below)
       GetLocomotionInput();
    }

    //this function is where the control of the character would happen
    void GetLocomotionInput() 
    {

    }
}