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 moonLite · Nov 02, 2012 at 08:51 AM · javascriptvariableclasstypecasting

Declaring & understanding Class in javascript

Hi Guys,

I wish to ask 2 questions about declare class and the concept of it, it also known as typecasting. The code is in javascript (unityscript)

From my own understanding, that class in javascript or unityscript is like having an array contain many items or having one big variable contains many small variables.

Example, example would be like Ball is a class has few properties ( a few variables, the weight, diameter & material used, condition of the ball; new or used)

1) Is my understanding of it correct? If not what would the more appropriate example of looking at it.

2) Assume my own understanding of it is correct, what is the proper or long term of defining class?

 var PlayerTimer : TimerClass = new TimerClass ( ) ;
 or 
 var PlayerTimer = new TimerClass ( ) ;



Below is my actual code:

 class TimerClass 
 {
     var FirstTimer : float = 0 ;
     var FirstTimerActive : boolean = true ;
     var SecondTimer : float = 0 ;
     var SecondTimerActive = true ;
  }
     
     var PlayerTimer : TimerClass ; // THIS IS THE ONE
  function Update ( )
  {..}
 
  function OnGUI ()
  {...}

3) Lets say for this one "`var PlayerTimer : TimerClass = new TimerClass ( ) ;`" I don't actually understand the statement of it. I tried to come out with my own understanding, like this:

 class BallCLASS
 {
  var condition
  var weight
  var diameter of the ball
  var price
 }
 
 var JohnSpecialBaseball : BallClass = new BallClass ():
 var NormalBaseBall : BallClass = new BallClass ():
 var BowlingBall : BallClass = new BallClass ();
 var   (A)       :  (B)      =  ( C)//for the asking and explaining below only

So to me it's like var a name or a certain ball (A) is actually a BallClass(B) type which is actually a NEW BallClass (C), which naturally (A) has the property of (B) since it's a new (C)
could I look at it this way?
This question is more like asking how a language statement is actually form with its noun and grammar etc.

So is my own understanding of declaring PROPERLY & LONG way of class correct? Please let me know if there's any more information I would need to know to understand everything about it, I'm still learning unity and really curious about it.

Thanks in advance for all the explanations!

Comment
Add comment · Show 4
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 Fattie · Nov 02, 2012 at 10:11 AM 3
Share

one small point. Note that with Unityscript.

Say you make a file called "happy.js"

IN FACT, it AUTO$$anonymous$$ATICALLY makes that a Class - even if you don't have a Class blah{ } inside it wrapping everything. Really it's quite confusing. It's one of those things that is simple but a bt confusing if you think on it too much :)

Regarding the deep question "What is a Class?" Nobody has a clue. Your definition is as good as anyone elses. When people try to talk about it epistemologically, they just get in to knots saying it is "an object" or something. If you read the books on deep OO theory, they're pretty simplistic to any working philosopher. So I'd totally forget that line of enquiry. :)

I really urge you just to USE THE$$anonymous$$ A LOT and then you will understand perfectly.

A really important word to use to sound important is:

INSTANCE

What you have above is a Class DEFINITION

when you do this,

var PlayerTimer : TimerClass = new TimerClass ( ) ;

you ARE ACTUALLY $$anonymous$$A$$anonymous$$ING ONE OF THE THINGS. (You've made a TimerClass "thingy" - so you get to use the fancy word "instance", You've "made an instance" of that Class.)

Enjoy!

avatar image AlucardJay · Nov 02, 2012 at 01:23 PM 0
Share

comment converted to answer

Fattie actually summerized it all up first with IN FACT, it AUTO$$anonymous$$ATICALLY makes that a Class - even if you don't have a Class blah{ } inside it wrapping everything.

That is what I was trying to explain by expanding how the JS script really is automatically a class.

avatar image moonLite · Nov 06, 2012 at 07:02 AM 0
Share

Thanks @alucardj & @Fattie , after both your comments, I totally have no questions about it! Thank you so much!

but i have one issue, so how do I mark it as answered? since both are only comments :D ( I have thumbed both up).

I think I will just post an answer, and say please see the comments below the questions :D If not, please let me know what I should do it more appropriately.

avatar image vexe · Oct 06, 2013 at 08:20 AM 0
Share

Ask whoever you want to accept his answer, to convert his comment to answer so that you can accept it :-)

1 Reply

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

Answer by AlucardJay · Nov 02, 2012 at 01:18 PM

Disclaimer : this is written by a noob, with just a little more than a dangerous amount of knowledge =]

Classes

The easiest way to imagine it is ... a class is a script.

If you create a new C# script, you'll see the format :

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour {
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     }
 }

If you create a JS, this is not seen :

 #pragma strict
 
 function Start () {
 }
 
 function Update () {
 }

but it is actually automatically set like :

 #pragma strict
 
 public class NewBehaviourScriptJS extends MonoBehaviour {
 
     function Start () {
     }
     
     function Update () {
     }
 }


Now when I say a class is a script, a script has variables :

 #pragma strict
 
 var timer1 : float = 0;
 
 function Start () {
 }

so this would be the the class :

 #pragma strict
 
 public class NewBehaviourScriptJS extends MonoBehaviour {
 
     var timer1 : float = 0;
     
     function Start () {
     }
 }

with your timers question, the class is just written into the main class/script :

 #pragma strict
 
 public class NewBehaviourScriptJS extends MonoBehaviour {
 
     class Timers {
         var timerCounter : float = 0;
         var timerMax : float = 5.5;
     }
     
     var timer1 : Timers = new Timers();
     
     function Start () {
     }
 }

Types and Typecasting

so if I call timer1.timerMax , this means I am calling variable timer1 (of type Timers) and within that class I am calling the variable timerMax (of type float).

Now looking back to your other qn with my flashlight example, this may make more sense thinking of how a class is a script (in this example).

I said if you have a GameObject, and attach a script to it (called FlashlightScript), then to be able to read the variables can call functions on this script, you can do :

 var myTorch : FlashlightScript;

This means I am declaring variable myTorch of type FlashlightScript.

just like a class, then the variables of the script can be called with

  myTorch.someBoolean = true; 
  var bl = myTorch.batteryLifeRemaining;

in this class example, this could be written as :

 #pragma strict
 
 public class PlayerScript extends MonoBehaviour {
 
     class FlashlightScript {
         var someBoolean : boolean = false;
         var batteryLifeRemaining : float = 5.5;
     }
     
     var myTorch : FlashlightScript = new FlashlightScript();
     
     function Start () {
     }
 }

I have probably confused the issue for you, but I am sure the gurus on this 'site will be happy to correct me and help you with follow up questions =]

Edit : Am simply glad my ravings helped in some way !

Happy Coding =]

Comment
Add comment · Show 2 · 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 StoneFish · Nov 15, 2012 at 02:40 AM 0
Share

Hi

I understand all of this post except for the " var myTorch : FlashlightScript = new FlashlightScript(); " part.....

Why is there a 'new' script being declared in the variable? What does it do or what's its significance?

Thanks

avatar image AlucardJay · Nov 15, 2012 at 02:47 AM 0
Share

The flashlight discussion was carried over from the OPs other question : http://answers.unity3d.com/questions/340976/pause-unpause-different-timers.html

As you say, it wouldn't be done to store a reference to a script, but above it is used just to reiterate how to declare and assign a new class to a variable.

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

14 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

Related Questions

How to Typecast JS Variables as C# Classes? 0 Answers

Enum style variable 2 Answers

Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer

Different way to access class variables? 1 Answer

Creating A Class Variable That Works With Any Class 0 Answers


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