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.
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.
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.
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);
@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.
This answer is unfortunately simply wrong. It would be completely impossible to do this performance-wise.
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.