• 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 Arnold Drusev · Aug 10, 2014 at 08:32 PM · java

Write values in list-like thing.

Hi guys!

The title may sound weird, but here is what i want. So in my game i have lots of objects and every object has a script called Info which contains a variable objectName. So how can i get this variable for every single object which is closer than some value to the player and make a list with the object. (Im making a space game and I got this idea from the EVE online UI)

Thanks. -Arnold

Comment
Add comment · Show 1
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 robertbu · Aug 10, 2014 at 09:19 PM 0
Share

Physics.OverlapSphere() will find all the colliders withing a specific radius.

If you are sure that every object has this script, you can just create a new, built-in array the same size one as the array returned by OverlapSphere(). If any of the object don't have this script, then use a collection that can grow. I recommend a .NET generic List. You will find info on many collection types including a generic List here:

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

You use GetComponent() to get the script from each collider in the array.

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

It takes some time to get your $$anonymous$$d wrapped around the concept, but you can do what you are seeking in just a line or two of code using LINQ. Some information on LINQ and Unity here:

http://unitygems.com/linq-1-time-linq/

1 Reply

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

Answer by chees502 · Aug 10, 2014 at 09:47 PM

If I am understanding your intention correctly. You could add a functionality on your info script to append itself to a static list. Doing it this way makes for better code than doing a search for all the GameObjects in the scene.

     using System.Collections.Generic;
     ...
     static IList<Transform> nearTargets=new List<Transform>();
     Transform playerPosition;
     float maxRange=10.0f;
     bool inRange=false;
     ...
     void FixedUpdate(){
         float distance=Vector3.Distance(playerPosition.position,transform.position);
         if(inRange){
             if(distance>maxRange){
                 inRange=false;
                 //remove from list
                 nearTargets.Remove(transform);
             }
         }else{
             if(distance<maxRange){
                 inRange=true;
                 //add to list
                 nearTargets.Add(transform);
             }
         }
     }


then you can just dot into it from any other code file with info.nearTargets[x].position;

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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Need to make a searching tool that can find gameObjects that contain the word the user inputs 1 Answer

Get the angle from one object to another. 0 Answers

Calling static jar function from Unity3D 0 Answers

Accessing and using Javaclasses from Unity 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