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 AlucardJay · Apr 04, 2012 at 06:39 AM · c#javascriptclassinstanceconvert

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

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 AlucardJay · Apr 04, 2012 at 07:08 AM 0
Share

... and a post-question : what is the difference between a public and static var ? (I've only used public and private (and hide in inspector public)).

avatar image kalvinlyle · Apr 04, 2012 at 09:18 AM 1
Share

http://answers.unity3d.com/questions/53538/public-private-and-static-variables-in-js.html

avatar image AlucardJay · Apr 04, 2012 at 09:25 AM 0
Share

Thanks, that helped clear up static vars. Still leaves me with how to create an Instance in JavaScript (which I now see static would make sure there is only one of these Instance's in existance).

2 Replies

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

Answer by AlucardJay · Apr 06, 2012 at 02:10 AM

Answered on this Question :

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

Comment
Add comment · 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
0

Answer by kalvinlyle · Apr 04, 2012 at 09:17 AM

 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() 
     {
 
     }
 }
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 09:30 AM 0
Share

Thanks for taking the time to show what the C# is doing. I should have explained better that I want to make the static (only one) Instance using JavaScript.

So far my scripts are working , using a different method of reading and writing to each other :

 public var myCharacterController : CharacterController;
 // public static var TP_Controller : Instance;
 public var TP_$$anonymous$$otor : Script_TP_$$anonymous$$otor;
 
 function Awake() 
 {
     myCharacterController = GetComponent("CharacterController");
     // Instance = this;
     TP_$$anonymous$$otor = GetComponent("Script_TP_$$anonymous$$otor"); // Warning , even with using -> OR transform.gameObject.GetCo... OR this.GetCo...
 }

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Serialization - Variables won't change on original construction 1 Answer

Need help converting Javascript to C# 3 Answers

Field is never assigned to, and will always have its default value 3 Answers

convert some c# to javascript please 0 Answers

making my classes usable? 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