- Home /
How to access 2d array in outside script
So here I have my code:
var pathScript;
var i;
var j;
function Start(){
pathScript = transform.parent.gameObject.GetComponent(nodecreator);
Debug.Log("I'm born!");
Debug.Log("i = " + i);
Debug.Log("j = " + j);
}
function OnTriggerEnter(other: Collider){
if (other.transform.tag == "wall"){
Debug.Log("Destroy Me");
pathScript.array[i,j] = null;
Destroy(transform.gameObject);
Debug.Log("I'm dead");
}
}
Unity gives me an error
MissingFieldException: UnityEngine.Transform[,].
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.ResolveMember ()
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory.CreateSetter ()
Boo.Lang.Runtime.RuntimeServices.CreateSetSliceDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<SetSlice>c__AnonStorey1E.<>m__15 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.DispatcherKey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.SetSlice (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.DynamicDispatching.SliceDispatcherFactory+<CreateSetter>c__AnonStorey13.<>m__6 (System.Object o, System.Object[] arguments)
Boo.Lang.Runtime.RuntimeServices.SetSlice (System.Object target, System.String name, System.Object[] args)
nodeDestroy.OnTriggerEnter (UnityEngine.Collider other) (at Assets/New Scripts/nodeDestroy.js:15)
How can I get this to work?
I did. That's what the debug logs are for in the start function. Another script assigns those variables when it's made.
Always type your variables. "`var i;`" is wrong, "`var i : int`" is correct.
That's all fine and good but it doesn't affect their assignment. And it doesn't have anything to do with accessing the 2d array in the other function.
I changed the code to this and it still gives same error:
var pathScript;
var i: int = 0;
var j: int = 0;
function Start(){
Debug.Log("I'm born!");
Debug.Log("i = " + i);
Debug.Log("j = " + j);
}
function OnTriggerEnter(other: Collider){
pathScript = transform.parent.gameObject.GetComponent(nodecreator);
if (other.transform.tag == "wall"){
Debug.Log("Destroy $$anonymous$$e");
pathScript.array[i,j] = null;
Destroy(transform.gameObject);
Debug.Log("I'm dead");
}
}
Answer by supernat · Nov 19, 2013 at 03:53 PM
Make sure the array in the other script is defined as something like this: public Transform[,] myArray;
Also, don't name the variable array. ;)
EDIT: After further thought, I think it is an issue with your variable definition. The variable you are referencing the script with is not specified with a type, so explicitly define your script reference variable type at the top. I would recommend always giving your variables types. JavaScript lets us be lazy, but someone else comes along to read your code, it can be confusing. Also, stuff like this happens.
How can I define the type when the type becomes the script name?
$$anonymous$$aybe it would help if I gave the code to my other script.
var node : Transform;
var width: int = 32;
var length: int = 42;
var array: Transform[,] = new Transform[width,length];
private var pathTarget: Transform;
function Start(){
for (i = 0; i < width; i++){
for (j = 0; j < length; j++){
var instance = Instantiate(node, Vector3(10*(width/2-width+1)+(i*10),0,10*(length/2-length+1)+(j*10)), Quaternion.Euler(0,0,0));
array[i,j] = instance;
instance.transform.parent = transform;
var child: nodeController = instance.gameObject.GetComponent(nodeController);
child.i = i;
child.j = j;
}
}
}
Weird, when I tried to declare the type as the scriptname before, unity would give me an error like it didn't recognize the type. But it works now, thanks guys! :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613122020im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D array problem in C# 2 Answers
Getting a 2d Sprite to move over time to an Array 1 Answer
Array of Tilemaps misbehaving 1 Answer
Animation = Trasnslate Speed 2 Answers