- Home /
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
Answer by whydoidoit · Oct 04, 2012 at 09:55 PM
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.
Hi $$anonymous$$ike,
I had already tried the System.Collections.Generic, but not the System.Linq. Just gave it ago and the same error pops up.
So, should it be at least possible to search/edit a List Array in another script? If so any advice?
Just tried this and it works fine for me... No need for either of the imports.
Oh hang on - scriptA is the name of the script right? You need to be using scriptALocation.attackerClonesArray.FindIndex(...
I tell you na$$anonymous$$g conventions really help - if that script had been effectively written as ScriptA then I'd have immediately known it was a type and not scriptALocation which is a variable. Seeing scriptA I'm guessing you thought of that as a variable access as I did.
Answer by Eric5h5 · Oct 05, 2012 at 06:57 PM
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.
Hi Eric5h5. I am going to explore this tonight / tomorrow. Looks like I am doing things the long way....
Your answer
Follow this Question
Related Questions
Brute Force Search Algorithm 0 Answers
A node in a childnode? 1 Answer
Selection list from Array Unity - Random - GameObjects array 1 Answer