• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ProtoTerminator · Nov 16, 2013 at 12:11 PM · 2darraydimensional

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?

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Nov 16, 2013 at 12:24 PM 0
Share

Put some information in to i and j.

avatar image ProtoTerminator · Nov 17, 2013 at 03:16 AM 0
Share

I did. That's what the debug logs are for in the start function. Another script assigns those variables when it's made.

avatar image Eric5h5 · Nov 17, 2013 at 03:39 AM 0
Share

Always type your variables. "`var i;`" is wrong, "`var i : int`" is correct.

avatar image ProtoTerminator · Nov 17, 2013 at 07:03 PM 0
Share

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.

avatar image ProtoTerminator · Nov 18, 2013 at 09:10 PM 0
Share

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");
     }
 }
 
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ProtoTerminator · Nov 19, 2013 at 09:41 PM 0
Share

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;
         }
     }
 }
avatar image Eric5h5 · Nov 19, 2013 at 10:33 PM 0
Share

The type is the script name.

avatar image ProtoTerminator · Nov 20, 2013 at 12:03 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D array problem in C# 2 Answers

How to Instantiate Objects at specific Spawn Points without overlapping? 0 Answers

Problems with 2d arrays , += incrementing values 0 Answers

2D Grid Question 0 Answers

Using a list of arrays to make Unity UI 2D button toggle colors 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges