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 Cherno · Aug 31, 2013 at 09:33 PM · list

Accessing List in another gameObject via script

I'm trying to iterate through all stored gameObject in a List and if a certain condition is met (in this case, if the gameObject is seen by the player), access another list in the current gameObject and add to it:

 var List1 : new List.<GameObject>(); //contains all Characters in scene
 
 var SeenBy : new List.<GameObject>();
     
 for (var CurrentObject : GameObject in List1)  
 { 
     CurrentObject.GetComponent(LineOfSight).SeenBy.Add(gameObject); //access the other objects SeenBy List and add this gameObject to it
 }
     


However, I get this error:

Assets/Scripts/LineOfSight.js(7,46): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(function(UnityEngine.GameObject): boolean)' was found.

What am I doing wrong? How do I access Lists or even normal variables (tried that too via GetComponent) while iterating through a List?

Comment
Add comment · Show 6
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 getyour411 · Aug 31, 2013 at 09:49 PM 0
Share

I'm confused because you show a line defining SeenBy as if it's in this script, then you say '..access the other objects SeenBy list..'

Expand the section at this site 'How to access variable or method...'

avatar image Cherno · Aug 31, 2013 at 09:52 PM 0
Share

Yes the code above is all in the script called LineOfSight, the idea is that both characters have this script and if character 1 sees character 2 then character 1 is added to character 2's SeenBy list :) (and vice versa)

The main point however, is how to access a gameObject's variables if it's currently being iterated as part of a list.

avatar image getyour411 · Aug 31, 2013 at 10:10 PM 0
Share

try something like foreach(GameObject go in List1) { go.variable1...

avatar image Cherno · Sep 01, 2013 at 04:58 PM 0
Share

After some more testing, I really don't understand how to access any variables (let alone lists) in the gameObject that's currently being iterated. For example, we have the folling main script "ExampleScript":

 import System.Collections.Generic;
 var ActorList : GameObject[];
 
 function Update()
 {
      ActorList = GameObject.FindGameObjectsWithTag ("Actor");
 }
 
 function CountActors()
 {
      for (var ActorCur : GameObject in ActorList)  
      { 
           ActorCur.GetComponent(ValueScript).Value = 20; //this line gives the error
      }
 }

and all Actors have this simple script called ValueScript:

 var Value : int = 10;


it still gives the error Assets/Scripts/ExampleScript.js(11,24): BCE0023: No appropriate version of 'UnityEngine.GameObject.GetComponent' for the argument list '(function(UnityEngine.GameObject): boolean)' was found.

avatar image Vonni · Sep 01, 2013 at 06:34 PM 0
Share

FindGameObjectsWithTag in Update, slow down there sailor :O!

Show more comments

1 Reply

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

Answer by fherbst · Sep 01, 2013 at 06:56 PM

Might it be that you have an additional method called "LineOfSight" which returns a boolean?

If yes, you could either rename that method or use

 GetComponent.<LineOfSight>()

to be more specific that you want the type, not let Unity figure out what LineOfSight is (like I said, the error message says that there is also a method with exactly this name).

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 Cherno · Sep 01, 2013 at 08:42 PM 0
Share

Derp, that was indeed the problem... I had a function named LineOfSight inside the script named LineOfSight! I feel kinda dumb now :[

Cheers!

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

19 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

Related Questions

A node in a childnode? 1 Answer

How to create a generic list from a group of gameObjects with specific tag? 1 Answer

Problem with getting resources and adding to list. (c#) 1 Answer

Can I use IENumerable.Repeat in Unity? 0 Answers

Check List<> for 'Item' 1 Answer


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