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 Catsby · Dec 29, 2010 at 04:56 PM · beginnerclassbce0044where-to-start-scripting

Questions about classes.

Hey guys I have two questions about classes in JS.

1) I see a lot of people using the term class and script interchangeably ( the actual .js files themselves). Is this a correct usage of the word?

2) I'm trying to write a classe in JS using examples and tutorials all over the community sites but Unity just seems to love giving me errors. It's my understanding that you can put all sorts of variables and functions inside of classes but I'm not having any success. Here's a bit from the .js file as an example;

class Fighter{

 //Fighter's stats
 ... // bunch of vars here for things like HP, Strength, etc.


 function OnGUI(){
     // the buttons that appear inside the 
     //abilities window and their functions  
     function Abilities (windowID){

         var AttackCommand = GUI.Button(Rect(10, 25, 60, 20), "Attack");

         if (AttackCommand == true){

             bEquations.AttackMonster(Fighter, Monster);
             Abilitieswinswitch = false;
             Timer = 1;
         }

         ... // more functions 

}

repeat for additional character classes like wizard, cleric etc. Now the problem is that Unity gives me a bunch of errors such as;

expecting } found 'function'.

expecting } found 'Abilities'.

';' expected insert semicolon.

Unexpected token: var

just about everywhere I have a function or variable inside of a function. Is this just a simple syntax error or am I violating rules?

Thanks in advance!

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

4 Replies

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

Answer by s4vi0r · Dec 29, 2010 at 06:37 PM

Howdy Catsby,

This link should help you a lot. Tells about unity javascript vs. traditional javascript, syntax classes.. whole nine yards.

http://www.unifycommunity.com/wiki/index.php?title=Head_First_into_Unity_with_JavaScript

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 Catsby · Dec 29, 2010 at 07:06 PM 0
Share

I see,

So making a dozen classes inside 1 .js file is not a good idea? Should I make 12 .js, one for each class, and attach them all to one game object called Characters? I've tried about a dozen work arounds for my problem and I think I'm at the point where I need a tutor or something. :/

avatar image
1
Best Answer

Answer by Jason B · Dec 29, 2010 at 08:37 PM

To answer one of your questions in short, yes, you should typically split up a lot of your work into several scripts where applicable.

In fact, if you're programming with C# and are integrated with Visual Studio, one of the first things you notice while working inside Visual Studio is that each script appears in the solution as a class, like it would if you were making a C# application from scratch, so that might be rather telling. Now, maybe class isn't the proper wordage, but each separate script can be used in a similar fashion.

The only thing is that each script isn't necessarily a self-contained prefab object, but it can hold data for your prefabs, working in tandem with prefabs to basically behave like a class.

But this is diving in too deep and the important thing to take from this is probably akin to what s4vi0r said. Each separate game object that has a script attached to it owns its own self-contained version of the script, which in my opinion makes game design logic far easier.

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 Catsby · Dec 29, 2010 at 08:51 PM 0
Share

The problem I'm facing now is moving everything I've done to game objects and how to use game objects in general.

avatar image
2

Answer by Eric5h5 · Dec 29, 2010 at 08:17 PM

You can't put functions inside other functions. Your Fighter class would have to derive from Monobehaviour in order to have a working OnGUI function in it, but it's unnecessary...in JS, the script is automatically a class that derives from Monobehaviour. So you can just name the script Fighter and it's a Monobehaviour-derived class called Fighter. In C#, you have to explicitly do this yourself. It's not a problem to have other classes in the script, but it depends on what you're doing whether it's appropriate...you wouldn't want Wizard or Thief in the Fighter script, for example; they should be separate scripts.

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
1

Answer by s4vi0r · Dec 29, 2010 at 08:21 PM

In response to your comment (was to big to fit in comment section)

Honestly, The games I normally make aren't complex enough for classes. I might use classes if I wanted to inherit some info from a class I had already created. Like a Person base class. Then I would inherit Person when I create Good Guy class and Bad guy class. Coming from C++ background I would normally do this.

I sort of just try to stay away from classes in Unity JS unless I think it will speed up my process.

I will just treat the script as the class. So if I make a Enemy Gameobject. I will make a script that has all of his members and functions.

Whats nice is I can prefab this enemy. So I can create another just like him with that script attached to it. And his script which holds his info is unique to him.

My enemy script might look like this:

var health : int;

 function Start()
 {
   health = 100;
 }

 function Update()
 {

 }

 function TakeDamage()
 {
   health -= 1;
   if(health == 0)
   {
     Destroy(this.gameObject);
   }
 }

I hope this helps.

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 Catsby · Dec 29, 2010 at 08:34 PM 0
Share

I think I might have figured out the problem. I have those ability functions for buttons inside an OnGUI function, which is inside the character classes but forgot that it's being called already inside OnGUI in the HUD script. I'll remove the OnGUI from the character classes and see if this works...

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

No one has followed this question yet.

Related Questions

What are the best tutorial videos to learn just Javascript? 1 Answer

Unity 3D Newbie Quetion in javascript. 2 Answers

I would like to learn a programming language. I have no prior experience in programing. What would you suggest for a beginner? 2 Answers

Are inner classes possible in Unity JavaScript? 1 Answer

Starting Unity/Programming from scratch 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