Accessing List Array in Other Script Problem

Hi,

I have a weird problem and keep bumping into it. I have 2 scripts, we will call them A and B. In script A I create a list array like this:

//Create Attacker Transforms Array
var attackerClonesArray = new List.<Transform>()

In script B I have function that needs to search the list array in script A.

However I seem unable to directly search the list array in script A. I always seem to have to create a new list array in script B, that is then populated by the list array in script A. Like this:

var scriptALocation : Component;
scriptALocation = GameObject.Find("Start").GetComponent(scriptA);

var attackerList = new List.<Transform>();
attackerList = scriptALocation.attackerClonesArray;

If I try to search the array directly I get the error:
Method not found: ‘System.Collections.Generic.List`1[[UnityEngine.Transform, UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]].FindIndex’.

The search function that works is:

attackerStatusScript.attackerListIndexNumber = attackerList.FindIndex (function (tr : Transform) tr.name == attackerStatusScript.transformName);

But the one that fails trying to access script A directly without creating another list array is:

attackerStatusScript.attackerListIndexNumber = scriptA.attackerClonesArray.FindIndex (function (tr : Transform) tr.name == attackerStatusScript.transformName);

However I am able to modify the list array directly in script A from script B, for example remove a Transform by index number.

Is this normal behaviour? I am trying not to have multiple copies of the same array kicking around as it seems inefficient.

I hope this makes sense.

Thanks

Paul

Hey Paul

That’s very odd - the code you’ve posted does not create a copy of the array - you’ve created a new array and then replaced it with a reference to the array on the first script (not created a copy). I’m guessing that this might be some weird UnityScript thing going on - perhaps with the definition of the list causing an invisible import of a namespace or something. You could try adding an import System.Linq and/or System.Collections.Generic to the script on B.

You could also just try defining any variable as a List of Transform in that script and then just access the scriptA object.

This is not really correct:

var scriptALocation : Component;
scriptALocation = GameObject.Find("Start").GetComponent(scriptA);

You don’t want to use Component, you want to use the actual type, which is scriptA in this case. Using Component may work with dynamic typing, but it’s not a good habit to get into. Also there’s no point declaring the variable on a separate line, just do

var scriptALocation = GameObject.Find("Start").GetComponent(scriptA);

That applies here as well:

var attackerList = new List.<Transform>();
attackerList = scriptALocation.attackerClonesArray;

Just do:

var attackerList = scriptALocation.attackerClonesArray;

If you’re not using scriptALocation for anything else, you might as well just use one line:

var attackerList = GameObject.Find("Start").GetComponent(scriptA).attackerClonesArray;

However…

I always seem to have to create a new list array in script B, that is then populated by the list array in script A.

The above code does not create a new List. It’s just a reference to the List in ScriptA. It’s preferable to do things this way so as to minimize the use of Find/GetComponent/etc., so you’re just using the reference instead of doing GetComponent all the time whenever you want to refer to that array. There are no “multiple copies of the same array” involved.