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*.returnSides() == d.returnSides() ){*
*pos = i;*
*break;*
*}*
*}*
*dList.RemoveAt(pos);*
*}*
*```*
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.
– anon34393514Thanks a lot, I'll check it out when I've completed some other stuff in my project. :)
– anon16956261