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 /
  • Help Room /
avatar image
0
Question by Determined · Feb 17, 2017 at 10:22 PM · prefabruntimeprefab-instance

How to detect if any prefab is instantiated in the scene from a script?

Hi,

I am working on a plugin. I need to detect from a Manager type script if any gameobject is instantiated in the scene at runtime. I also need to grab the newly instantiated gameobject. Without adding any script in the prefab.

Like this:
FixedUpdate(){
if ( AnObjectInstantiated() ) {
GrabTheInstantiatedGO();
DoSomething();
}

How can I do this?

Thanks in advance.

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
0
Best Answer

Answer by mmciver · Feb 18, 2017 at 04:15 AM

It is unclear how much control you have over what may be instantiated, but if you do have control over that, then tagging the possible prefabs and using https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html would work. Otherwise, https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html.

Without deeper methods being uncovered, the somewhat rude solution would be to compare the list of active objects with what the list of active objects used to be. The difference is anything new.

Keep in mind that both of these are performance heavy, so using them every fixedupdate would be significantly costly, and isn't recommended.

This is obviously much faster and cheaper if you do have access to the scripts within the project.

Hopefully someone has a better answer for you.

Comment
Add comment · Show 4 · 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 Determined · Feb 18, 2017 at 05:55 AM 0
Share

Thanks a lot for some direction. The first one would be really heavy. I have also thought of a second solution like the one you said. Saved it as a last resort. I was trying to make the plugin as user friendly as I can, so I was trying to avoid the need of attaching scripts by user manually. But seems like there's no efficient way to do so.

Now, I am thinking that, I would just write a script for the user to put it over the prefabs, that need to use the manager type script, which will just call a method inside OnEnable/Start.

avatar image mmciver Determined · Feb 18, 2017 at 06:21 AM 0
Share

Another option would be that if someone is using the plugin, they understand that they have to specifically . If your plugin has a static class to handle the data, having the user add a method for something like

 Plugin side:
 void NewObject(GameObject object) {
   //do something;
 }
 
 
 
 User side:
 Plugin plugin = new Plugin();
 plugin.NewObject(gameObject);
avatar image Determined mmciver · Feb 18, 2017 at 08:15 AM 0
Share

@mmciver Yeah, that is also a good idea.
But, I think only dragging a script into the GameObjects/Prefabs will be more easier for maximum users. ( Though I don't know if anyone would use it :P ).
Anyway, It's working now.
If anyone is interested, I am just developing an asset for creating $$anonymous$$imap and planning to post it for free in the asset store.

avatar image Fattie · Oct 06, 2018 at 03:05 PM 0
Share

This answer is unfortunately simply wrong. It would be completely impossible to do this performance-wise.

avatar image
2

Answer by merkaba48 · Jul 13, 2017 at 05:22 PM

If it's possible for you to change the code that instantiates the objects, you could write your own go-between instantiate method that also calls event, then have whatever you need to know about new objects subscribe to that event. E.g.

 public static class ExtensionMethods
 {
     public delegate void SpawnAction(Object newObj);
     public static event SpawnAction OnSpawn;
 
     public static Object SpawnObject(Object obj, Vector3 position, Quaternion rotation)
     {
         Object newObj = MonoBehaviour.Instantiate(obj, position, rotation);
         if (OnSpawn != null)
             OnSpawn(newObj);
 
         return newObj;
     }
 }

 public class SomeComponent : MonoBehaviour
 {
     void OnEnable()
     {
         ExtensionMethods.OnSpawn += ObjectSpawned;
     }
     void OnDisable()
     {
         ExtensionMethods.OnSpawn -= ObjectSpawned; // Prevent memory leaks by cleaning up event subscription
     }
     void ObjectSpawned(Object newObj)
     {
         Debug.Log("Object was spawned: " + ((GameObject) newObj).name);
     }
 }

Then, rather than use Instantiate() to spawn objects you want to track, use ExtensionMethods.SpawnObject()

I should note that I am still learning a lot myself...so there might be some catches to this method I don't know about.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I detect prefabs edited in the prefab editor in OnValidate? 2 Answers

UI changed in game affects prefab instances 0 Answers

Unity overwrites prefab values in runtime 0 Answers

I have two Background Prefab. I want to generate and spawn them at Rutime in alternative ways..like Bg1..Bg2..Bg1..Bg2....thats way. Also in smooth manner. This will happen in canvas only... I will give my code following....Please suggest me... 0 Answers

How to make a prefab a parent to other prefabs? 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