- Home /
passing multi-dimensional arrays from one script to another?
ok so i have a map layout: 125*JS GridMaker*125 var instantiateObject : GameObject;
var gridX = 10;
var gridY = 10;
function Start () {
var map : GameObject[,] = new GameObject[gridX,gridY];
for ( var x = 0; x < gridX; x++){
for (var y = 0; y < gridY; y++){
var myName = Instantiate(instantiateObject, Vector3(x *.5,0,y * .5), Quaternion.identity);
myName.name = "x: " + x + "y: " + y;
map[x,y] = myName;
//Debug.Log(map[x,y]);
}
}
}
and i have a instantiate where i am trying to acces that maps gameObjects: JS GuiStart
// assign my grid point
function Start () {
middleOfMap = GetComponent(GridMaker);
leftMiddle = middleOfMap.Map[1,1];
}
function OnGui () {
//If gui button is down Instantiate monster one at gridpoint
if(GUI.Button (Rect (Screen.width - (Screen.width - 10) + monsterOneRight,Screen.height - 65,50,50), "Monster_1")){
Instantiate(monster1,leftMiddle,Quaternion.identity);
}
but it gets confused about which gameObject i am trying to acces in my array?
Answer by aldonaletto · Oct 23, 2011 at 06:04 AM
You should move the map declaration outside Start - variables declared inside a function are local, and nobody outside knows them.
var gridX = 10; var gridY = 10; var map : GameObject[,] = new GameObject[gridX,gridY];
function Start () { ... In the GuiStart, you must define the type of middleOfMap as GridMaker. If both scripts are attached to different objects, you must have a reference (gameObject, collider, transform, etc.) to the GridMaker owner. You may also have problems because you're reading the map value at Start, but nobody can guarantee that GridMaker's Start function will be executed prior to GuiStart - read the map value in OnGUI instead:
var middleOfMap: GridMaker;
function Start () { middleOfMap = GetComponent(GridMaker); // both scripts must be attached to the same object! }
function OnGui () { //If gui button is down Instantiate monster one at gridpoint if (GUI.Button (Rect (Screen.width - (Screen.width - 10) + monsterOneRight,Screen.height - 65,50,50), "Monster_1")){ var leftMiddle = middleOfMap.map[1, 1].transform.position; // read the map value here Instantiate(monster1,leftMiddle,Quaternion.identity); }
}
now its giving me this error:
No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, UnityEngine.Transform, UnityEngine.Quaternion)' was found
i dont get it cause i have been using this exact same formula through out my entire game...
ok so i found my real problem, i did a debug.log of my left$$anonymous$$iddle var and it says it cant find my array. well this is actually what it says
Field 'Grid$$anonymous$$aker.map' not found.
These scripts - Grid$$anonymous$$aker and GuiStart - are attached to the same object? If not, you must have a reference to the Grid$$anonymous$$aker object in GuiStart - kind of:
var gridObject: Transform;
And drag the object to which Grid$$anonymous$$aker is attached to this variable. Then you must change the first line of Start to :
middleOf$$anonymous$$ap = gridObject.GetComponent(Grid$$anonymous$$aker);
no they are on the exact same object named controller.
Have you used exactly the script GuiStart above? Because Unity is complaining that a Transform is being passed as the second argument of Instantiate, but the line below:
var left$$anonymous$$iddle = middleOf$$anonymous$$ap.map[1, 1].transform.position;
forces left$$anonymous$$iddle to be a Vector3, since it's receiving middleOf$$anonymous$$ap.map[1,1].transform.position. You can define left$$anonymous$$iddle as a Vector3:
var left$$anonymous$$iddle: Vector3 = middleOf$$anonymous$$ap.map[1, 1].transform.position;
You must also check if Grid$$anonymous$$aker is correct:
var gridX = 10; var gridY = 10; // this line must be here, not inside Start! var map : GameObject[,] = new GameObject[gridX,gridY];
function Start () { for ( var x = 0; x < gridX; x++){ for (var y = 0; y < gridY; y++){ var myName = Instantiate(instantiateObject, Vector3(x .5,0,y .5), Quaternion.identity); myName.name = "x: " + x + "y: " + y; map[x,y] = myName; //Debug.Log(map[x,y]); } } }
Your answer
Follow this Question
Related Questions
How to make an Array equal the Components of an Instantiated GameObject 1 Answer
Get unity to recognize prefab in C# 2 Answers
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers
How to Instantiate a Different Game Object After Getting to the End of an Array? 1 Answer
Instantiate an array of prefabs? 0 Answers