Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheMorten · May 14, 2010 at 01:27 PM · javascriptclassstructure

Making a class publicly available

Hey, I have some programming experience but only in traditional environments (Eclipse, VisualStudio).

I need to create a class called Die (as in the singular of "dice"), that I can use across different JavaScript files. The class is supposed to represent one die, that can be rolled by calling (for example) die1.roll().

I have a MouseInformer script, which takes care of what the mouse has "picked up". When I click on a button (with a picture of a die) I want that button to add a die to MouseInformer's array of dice.

In the end, I want to be able to click on a text box, into which the dice list is emptied and all the dice are rolled, and shown in the text box.

I am really not used to the programming structure of Unity and find it quite difficult to imagine how my scripts work, so apologies if this question is really basic or if I'm going about the problem the wrong way (if you have any suggestions, please write!).

I want to know how to make the Die class visible to other JavaScript files in the project, such as the chat-box, so that I can transfer the dice between arrays in different scripts.

I've attached my MouseInformer script:

var dList = new Array(); var pos : int;

//The Die class which contains data on how many sides it has class Die { var sides : int;

 function Die(n : int) {
     sides = n;
     Debug.Log("1d"+sides+" created");
 }

 function roll() {
     return parseInt(Random.Range(1,sides)) ;
 }

 function returnSides() {
     return sides;
 }

}

//Add a Die of a certain type to the dicelist (dList) function AddTo (d : Die) { dList.Push(d); }

//Removes a Die of the same type as the one given in the input function Remove (d : Die) { for (i=0; i<dList.length ; i++) { if (dList[i].returnSides() == d.returnSides() ){ pos = i; break; } }

 dList.RemoveAt(pos);

}

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

3 Replies

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

Answer by Novodantis 1 · May 14, 2010 at 01:41 PM

I believe a .js script is treated as a class in Unity, and to reference it you only need have the script in your project. You can then write lines such as

var myObject : Die;

in another .js file, and it will recognise you are creating a new instance of a Die.js component. You can assign this a value in various ways; the easiest being to attach this other script to an object, then dragging onto it a Game Object that has a Die component attached. You can access any function or variable not specified as private, like this

var result = myObject.Roll();
myObject.numberOfSides = 8;
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 Novodantis 1 · May 14, 2010 at 01:45 PM 0
Share

Of course, there's nothing to stop you from saying something like: var myObject : Die = new Die(); Rather than use a GameObject with a Die component.

avatar image TheMorten · May 15, 2010 at 11:24 AM 0
Share

Thanks a lot, I'll check it out when I've completed some other stuff in my project. :)

avatar image
1

Answer by duck · May 14, 2010 at 01:45 PM

All public classes declared in Unity scripts are available to all other scripts, with the exception of certain compilation order rules.

So to create and use a new Die instance, in any other js script, just do:

var thisDie = new Die(8);
var rollResult = thisDie.Roll();

Incidentally, if you're familiar with coding in other environments such as VS and Eclipse, you might find you get on a lot better using C# in Unity instead of Javascript. You can even use VS to work on the scripts if you like!

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 Fappp · Jan 30, 2016 at 08:41 AM

 public static class Die{
     public function Roll( sides : int) : int{
         var result = Random.Range(1, sides);
         return result;
     }
 }

Then from any script:

 rollDiceResult = Die.Roll( sides );
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

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

1 Person is following this question.

avatar image

Related Questions

Why can't i access my c# class from javascript 1 Answer

Creating a dynamic array of objects of custom class 1 Answer

Type 'Object' does not support slicing 1 Answer

Different way to access class variables? 1 Answer

How do I access a variable declared outside a class? 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