Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Severenger1 · Oct 21, 2020 at 11:02 AM · componentdynamicperformance optimizationdistance check

Dynamically getting nearby objects with component

Hi, I'm looking for a performant way of getting all nearby objects with a specific class.

An example of what I'm trying to do: I (the player) have a power cord. I want to turn on a light above all nearby wall sockets to indicate where I can plug the power cord into. Only sockets near me (within 10 units for example) need to turn on as I don't care about ones in the distance. If I move too far from a nearby socket it should turn off and vice versa if I move close to another. Sockets can be instantiated/destroyed at any point while playing.

From my understanding using FindObjectsOfType and checking their distance from the player every frame isn't too friendly on performance. Since these sockets can be spawned/despawned during play, getting a list on Awake or when the power cord is selected wouldn't work as far as I'm aware?

Any suggestions? Cheers.

Comment
Add comment · Show 2
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 Klarzahs · Oct 21, 2020 at 12:29 PM 1
Share

You can increase the performance of your checks by sorting them in 3d space. One of your options would be a Quadtree, which divides your world into cubes. If you then check only cubes which are neighbours (or up to 10 units away), you essentially pick only a very small portion of your overall objects

avatar image Severenger1 Klarzahs · Oct 22, 2020 at 12:33 AM 0
Share

Looked up quadtrees and while it might be a bit much for me to wrap my head around, it does seems like a good solution, thanks!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by makled · Oct 22, 2020 at 12:57 AM

Hey,

There are two solutions for your problem.

Trigger Box

You can place a sphere collider (Set trigger to true) that is 10 units in diameter around the player. You can place this object as a child of the player. The next step would be to identify these sockets. You can do that by getting a list of all object the objects the trigger is colliding with.

Player Singleton

The other solution is to add a script to the player that makes it a singleton and allow other scripts to access it easily without using "FindObjectOfType". Here is how you do it:

  1. In your player script create a singleton the following way:

    using System.Collections; using System.Collections.Generic; using UnityEngine;

       public class PlayerSingletonScript : MonoBehaviour
         {
           
             public static PlayerSingletonScript Instance;
             
             void Awake()
             {
                 if (Instance == null)
                 {
                     Instance = this;
                 }
                 else
                 {
                     Destroy(this.gameObject);
                 }
             }
     
     //Rest of your code
    
    

Now this will help you to access your gameobject easily from any other script on the screen this way "PlayerSingletonScript.Instance" since Instance is a static variable.

Adding a script "SocketObject.cs", for example, that would calculate the distance between itself and PlayerSingletonScript.Instance.transform.position and accordingly doing the action itself.

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.

Follow this Question

Answers Answers and Comments

141 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Is there a way to dynamically attach a script to a GameObject during runtime? 1 Answer

declaring unknown script components under a empty variable 0 Answers

Glowing effects in Unity 0 Answers

Implicit conversion doesn't work with Component. Is it bug? 0 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