Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AurraSing · Jan 30, 2020 at 04:47 AM · findobjectsoftype

How to keep track of all objects of same type?

I want to have a list of TrailRenderer that keeps track of all the trail renderers so that I can do operations on all of them without using "FindObjectsOfType" (because I heard Find is slow). My question is: How do I get notified when a new trail Renderer is instantiated or is destroyed so that I know when to add or remove from the list? Is there a call-back function like "TrailRenderer.OnNewTrailRendererInstantiate?"

Comment
Add comment · Show 3
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 sacredgeometry · Jan 30, 2020 at 07:44 AM 0
Share

Then do that? I dont understand what you are asking you literally typed out that you wanted. So create a collection/ array of TrailRenderers then and when you create a new object with one then add it to the collection.

avatar image AurraSing sacredgeometry · Jan 30, 2020 at 08:06 AM 0
Share

$$anonymous$$y question was not clear, I updated it :)

avatar image xxmariofer AurraSing · Jan 30, 2020 at 02:24 PM 0
Share

Hello, thrre is no callback function at all, but trail renderers are allways attached to momobehaviour and they do have Awake and OnDestroy methods, so one of the most efficients ways of achieving what you need is creating another script that works as a mánager, A$$anonymous$$D one script for each trail obj that adds a reference to the manager when it gets instantiated A$$anonymous$$D removemos the reference when it gets destroyed

3 Replies

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

Answer by unity_ek98vnTRplGj8Q · Jan 30, 2020 at 03:59 PM

To clarify, you probably want something along these lines

 //In your script that tracks all the trail renderers
 public class TrailRendererManager : Monobehavior {
     public static List<TrailRenderer> trailList;
 
     void Awake() { 
         trailList = new List<TrailRenderer>();
     }
 }
 
 //Attached to every object with a trail renderer
 public class TrailRendererInstance : Monobehavior {
     void Start(){ 
         TrailRendererManager.trailList.Add(GetComponent<TrailRenderer>());
     }
 }
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 Captain_Pineapple · Jan 30, 2020 at 05:47 PM 0
Share

This but depending on what you use the trail it should also contain

  void OnDestroy()
 {
      TrailRenderer$$anonymous$$anager.trailList.Remove(GetComponent<TrailRenderer>());
 }

otherwise you might have a shitton of nullrefs in that list.

avatar image
1

Answer by dan_cmj · Jan 30, 2020 at 05:57 PM

Currently on my game I'm using something along these lines. This takes care of the Add / Remove. If you want to be more specific, you can use OnDestroy instead of OnDisable

 public class TrailRenderer : MonoBehaviour {
     private static List<TrailRenderer> _TrailRendererList = new List<TrailRenderer>();
     public static List<TrailRenderer> TrailRendererList {
         get {
             return _TrailRendererList;
         }
     }
     void OnEnable() {
         _TrailRendererList.Add(this);
     }
 
     void OnDisable() {
         _TrailRendererList.Remove(this);
     }
 }





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

Answer by xxmariofer · Jan 30, 2020 at 07:42 AM

 void Awake()
 {
    var trails = FindObjectsOfType<TrailRenderer>();
 }

trails would be an array with all the trailrenderers

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

121 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

Related Questions

Why is FindObjectOfType(MyType) not finding anything? 2 Answers

How do I find a GameObject by a script attached to it? 2 Answers

FindObjectsOfType not find all objects and code not do anything 0 Answers

Where is ScriptableObject.CreateInstance stored? 2 Answers

FindObjectsOfType for abstract generic class 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