- Home /
make a variable for an array from another script ?
i was curious if it was possible to make a variable for an array from another script. so i dont have to type something like this over and over again
wordDBScript.Root[1];
and make it like
wordTemp[1];
Answer by DannyB · Jun 21, 2013 at 10:43 AM
I am not sure if this works in Javascript, but in C#, yes you can. This is sometimes called square brackets operator overloading, or Indexers.
Basically, you need to define an overload function that handles all requests to YourClass[]
Attach this script to an empty GameObject to see it work:
using UnityEngine;
using System.Collections;
public class GeneralTest : MonoBehaviour {
public class ChildClass {
string[] myStringArray = new string[] { "Hello", "World" };
// This is the overload. Return a string whenever
// MyChild[int] is called
public string this[int index] {
get { return myStringArray[index]; }
set { myStringArray[index] = value; }
}
}
void Start() {
ChildClass child = new ChildClass();
Debug.Log( child[0] + " " + child[1] );
child[0] = "Tada";
Debug.Log( child[0] + " " + child[1] );
}
}
More info:
Indexers on MSDN (I hate that site, but its the official manual...)
Indexers on DotNetPerls (the C# guide for the rest of us)
A related answer on StackOverflow
im having some trouble applying the code above.
I actually got a stackoverflowexception. probably because im declaring it and the array from script B isnt handing it over. let me re-explain what i meant Script B as an array I want and Script A will present the array in the buttons on the GUI. The only way I got the buttons to show the words from Script B is to declare an instance and call the instance (wordDBscript.Root, root being the array variable).
im just curious if i can shorten "wordDBscript.Root[1]" to something like "wordtemp[1]" Script A: using UnityEngine; using System.Collections;
public class LevelScript : $$anonymous$$onoBehaviour {
float ButtonWidth = (Screen.width)/5;
float ButtonHeight = Screen.height/50 +Screen.height/25;
float ScreenToButtonWidth = Screen.width/25;
float RowOneButtonPosY;
float RowTwoButtonPosY;
public string[] visibleRoots = new string [8];
GameObject wordDB;
WordDatabaseScript wordDBScript;
// Use this for initialization
void Start ()
{
RowOneButtonPosY = (Screen.height*3)/4;
RowTwoButtonPosY = (Screen.height*3)/4+ButtonHeight+Screen.height/30;
//visibleRoots = GameObject.Find("_WordDatabase").GetComponent<WordDatabaseScript>().Root;
}
void Awake()
{
wordDB = GameObject.Find("_WordDatabase");
wordDBScript = wordDB.GetComponent<WordDatabaseScript>();
//System.Array.Copy(wordDBScript.Root,visibleRoots,8);
}
// Update is called once per frame
void Update () {
print(ButtonWidth);
print(ButtonHeight);
}
void OnGUI()
{
GUI.Button(new Rect(ScreenToButtonWidth,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[0]);
GUI.Button(new Rect((ScreenToButtonWidth*2)+ButtonWidth,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[1]);
GUI.Button(new Rect((ScreenToButtonWidth*3)+ButtonWidth*2,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[2]);
GUI.Button(new Rect((ScreenToButtonWidth*4)+ButtonWidth*3,RowOneButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[3]);
GUI.Button(new Rect((ScreenToButtonWidth),RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[4]);
GUI.Button(new Rect((ScreenToButtonWidth*2)+ButtonWidth,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[5]);
GUI.Button(new Rect((ScreenToButtonWidth*3)+ButtonWidth*2,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[6]);
GUI.Button(new Rect((ScreenToButtonWidth*4)+ButtonWidth*3,RowTwoButtonPosY,ButtonWidth,ButtonHeight),wordDBScript.Root[7]);
}
void ButtonAssign()
{
}
}
ScriptB:`using UnityEngine; using System.Collections;
public class WordDatabaseScript : $$anonymous$$onoBehaviour { public string [] Root;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
BeginnerStage();
}
void BeginnerStage()
{
Root = new string[] {"one","two","three","four","five","six","seven","eight"};
}
} `
I am not sure where are you having difficulties... are you simply looking for a shorter way to write things in your script?
If you do:
string[] wordtemp = wordDBScript.Root;
You will be able to use wordtemp[0]
funny story, yes and I thought of that line of code above this comment and it didnt work at first then i was like maybe i put it in the wrong spot just now and it works fine. Your bracket overloading idea may become of use to me later, thank you for your help