1 one GameObject ‘Manager’ with don’t destroy on load and and one var level01Words : List<String>;
for each Level and a function FeedWord
that takes level : int
and word : String
as Parameters. The Function then tests what level it got as input (you can do it with if-statements, but I’d rather search for switch/case). depending on level, add the word to the appropriate levelWords-List.
2 one empty GameObject in each Scene that cares for the word-creation.
needs a variable to store the reference to the ‘Manager’-Script. Search the reference in Start()
. (to get the reference, google gameObject.Find
and GetComponent
and/or ‘access variables from another script’ or something).
when the word is done, feed it to Manager by calling it’s 'FeedWord’Function.
3 the same empty GameObject can have RevisionControl. This needs to reference the ManagerScript and read its Lists, then display them as buttons.
Edit: Oh, well… -.- here’s the code then…
//ManagerScript.js:
var level01Words : List.<String> = new List.<String>();
var level02Words : List.<String> = new List.<String>();
// an new List of Strings is created and assigned to 'lvl01Words'.
// this List is currently empty.
function Awake ()
{
DontDestroyOnLoad(transform.gameObject);
// have this GameObject survive throughout all scenes.
}
/**************** what the following function does
we need a function that will later feed the Words into the empty Lists
we create a function that take two 'Parameters'
Parameters are like variables, just that they exist only inside their function.
we have to say, of what type these Parameters are
for some weird reason, they are separated by ',' insteat of ';'
when we call the function, we have to give the appropriate variables in the right order
(can't call: FeedWord(someString, someInt);
but call: FeedWord(someInt, someString);
when calling the function, the values are assigned to the parameter-variable
so inside the function FeedWord the variable 'level' has now the value 'someInt'
and 'word' has now the value of 'someString' and the function will process that...
**************************/
function FeedWord (level : int, word : String)
{
switch (level)
{
case 1:
level01Words.Add(word);
break;
case 2:
level02Words.Add(word);
break;
default:
print("we are neither in lvl 1 nor 2...?");
break;
}
}
```
//WordCreation.js
var level : int; // set this value in the editor (level1 = '1', level2 = '2' etc.)
private var newWord : String;
private var managerScript : ManagerScript;
// the variable managerScript references the path to a Instance (=Copy)
// of your self-defined Class 'ManagerScript'
function Start()
{
managerScript = gameObject.Find("Manager").GetComponent(ManagerScript);
// if possible, reference-paths are always stored in function Start
// depending on how you create the word, you might want to call
// function CreateWord in function Start, in function Update or from another Button etc.
/****** CreateWord();
just commented out for demonstration.
Once the CreateWord-Function works propperly
(= you have added the code that actually creates a word),
you have to remove the *******/
managerScript.FeedWord(level,"Word1");
managerScript.FeedWord(level,"Word2");
managerScript.FeedWord(level,"Word3");
}
function CreateWord ()
{
// do stuff to create your word. When it's done:
//newWord = the newly created word.
managerScript.FeedWord(level, newWord); // adds newWord to the list of level-x-Words in Manager.js
}
```
//RevisionControl.js
var level : int;
// again, set in editor to respective value.
// for final revision, set this to any other value. (e.g. '100')
private var manager : ManagerScript;
function Start ()
{
manager = gameObject.Find("Manager").GetComponent(ManagerScript);
}
function OnGUI ()
{
switch (level)
{
case "1":
DisplayButtons(manager.level01Words);
break;
case "2":
DisplayButtons(manager.level02Words);
break;
default:
// Default assumes we are in final revision and displays ALL words:
DisplayButtons(manager.level01Words);
DisplayButtons(manager.level02Words);
break;
}
}
function DisplayButtons (levelWords : List.<String>)
{
for (var i : int = 0; i< levelWords.Count; i++)
// create a new variable 'i : int' to use it inside the loop
// take 'element 0' (= the first word in the List) and do what is inside the loop,
// then take the next element, do what's inside the loop,
// ... and so on, until you are through with all elements
{
if (GUI.Button (Rect((10 + (level-1)*60), (10 + i*30), 50, 20), levelWords*))*
_/**** what this last line of code does:_
_(10+(level-1)*60) sets the x-value of the topleft corner of the new button._
_if 'level' is 1, then this position is at (10+(1-1)*60) = 10._
*so all words of level1 draw in the first column at x-position = 10.*
_if 'level' is 2, then this position is at (10+(2-1)*60) = 70._
*so all words of level2 draw in the second column at x-pos = 70.*
_(10+i*30) sets the y-value of the topleft corner of the new button._
*i = the first, second or third etc Word in the List we are currently checking.*
_If i = 0 (=first word), then this pos is at (10+0*30) = 10._
*So all first words always draw in the top row at y-pos = 10.*
_If i = 1 (=second word), then this pos is at (10+1*30) = 40._
*So all second words always draw in the second row at y-pos = 40.*
_levelWords*:*_
_*if i = 0 that's the first Word of the current String-List.*_
_*if i = 1 that's the second Word and so on.*_
<em>_**********************/_</em>
_*// if this button we just created gets clicked, do:*_
_*{*_
_*// load the explaination-page depending on what 'theWord' is*_
_*}*_
_*}*_
_*}*_
_*```*_
_*Greetz, Ky.*_