Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 code-blep · Oct 04, 2012 at 08:32 PM · arraylistsearchother script

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

Comment
Add comment
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

2 Replies

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

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.

Comment
Add comment · Show 14 · 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 code-blep · Oct 05, 2012 at 06:46 PM 0
Share

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?

avatar image whydoidoit · Oct 05, 2012 at 06:48 PM 0
Share

Yes - that should absolutely be possible.

avatar image whydoidoit · Oct 05, 2012 at 06:53 PM 0
Share

Just tried this and it works fine for me... No need for either of the imports.

avatar image whydoidoit · Oct 05, 2012 at 06:55 PM 0
Share

Oh hang on - scriptA is the name of the script right? You need to be using scriptALocation.attackerClonesArray.FindIndex(...

avatar image whydoidoit · Oct 05, 2012 at 06:58 PM 2
Share

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.

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · 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 code-blep · Oct 05, 2012 at 08:21 PM 0
Share

Hi Eric5h5. I am going to explore this tonight / tomorrow. Looks like I am doing things the long way....

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.

Follow this Question

Answers Answers and Comments

11 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

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

How to see what element is the current one 2 Answers

Create Array of Transforms and Detect Distance 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges