Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by JBoy · Jan 02, 2013 at 01:43 AM · objectsendmessageinactive

SendMessage() to inactive object?

how do I gameObject.SendMessage() to an inactive game object?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jan 02, 2013 at 01:50 AM

Simple answer: You can't.

A deactivated gameobject does not receive any messages from Unity. Deactivated equals "not there" for the engine.

Comment
Add comment · Show 3 · 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 yoyo · Nov 12, 2014 at 08:39 PM 0
Share

Which is unfortunate -- there really ought to be Send$$anonymous$$essageOptions.IncludeInactive.

avatar image fafase · Nov 12, 2014 at 08:46 PM 0
Share

You can still call methods on a script, inactive means that the Update/FixedUpdate and other $$anonymous$$onoBehaviour methods and callbacks are not run but if you have the reference to the script then you can call any method since the object is in memory.

avatar image Bunny83 · Nov 12, 2014 at 10:25 PM 0
Share

Right, you can for example use an interface on the target class and use GetComponent with the interface. Of course you can't use the default generic GetComponent as it has a constraint to "Component". However, you can add an extension method that does the same but also works with interfaces:

 public static T GetInterface<T>(this GameObject aObj)
 {
     return (T)aObj.GetComponent(typeof(T));
 }
 
 public static T GetInterface<T>(this Component aObj)
 {
     return (T)aObj.GetComponent(typeof(T));
 }

With those in a static class you can do this:

 public interface I$$anonymous$$yInterface
 {
     void $$anonymous$$yInterface$$anonymous$$ethod();
 }
 
 // [...]
 
 public class Some$$anonymous$$onoBehaviour : $$anonymous$$onoBehaviour, I$$anonymous$$yInterface
 {
     // [...]
     public void $$anonymous$$yInterface$$anonymous$$ethod()
     {
         Debug.Log("Yay");
     }
 }
 
 // [...]
 // Somewhere else:
 
 I$$anonymous$$yInterface mi = someGameObject.GetInterface<I$$anonymous$$yInterface>();
 if (mi != null)
     mi.$$anonymous$$yInterface$$anonymous$$ethod();


Another alternative is to use reflection on your own ;) Iterate through all components inherited from $$anonymous$$onoBehaviour and check if they contain a method with the given name.

avatar image
2

Answer by gsabran · Dec 03, 2015 at 08:29 AM

Here is how I solved it, inspired by @Bunny83:

 using UnityEngine;
 using System;
 using System.Reflection;
 
 namespace MessengerExtensions {
     
     /// <summary>
     /// Broadcast messages between objects and components, including inactive ones (which Unity doesn't do)
     /// </summary>
     public static class MessengerThatIncludesInactiveElements {
 
         /// <summary>
         /// Determine if the object has the given method
         /// </summary>
         private static void InvokeIfExists(this object objectToCheck, string methodName, params object[] parameters)
         {
             Type type = objectToCheck.GetType();
             MethodInfo methodInfo = type.GetMethod (methodName);
             if (type.GetMethod (methodName) != null) {
                 methodInfo.Invoke(objectToCheck, parameters);
             }
         }
         
         /// <summary>
         /// Invoke the method if it exists in any component of the game object, even if they are inactive
         /// </summary>
         public static void BroadcastToAll(this GameObject gameobject, string methodName, params object[] parameters) {
             MonoBehaviour[] components = gameobject.GetComponents<MonoBehaviour> ();
             foreach (MonoBehaviour m in components) {
                 m.InvokeIfExists(methodName, parameters);
             }
         }
         /// <summary>
         /// Invoke the method if it exists in any component of the component's game object, even if they are inactive
         /// </summary>
         public static void BroadcastToAll(this Component component, string methodName, params object[] parameters) {
             component.gameObject.BroadcastToAll (methodName, parameters);
         }
         
         /// <summary>
         /// Invoke the method if it exists in any component of the game object and its children, even if they are inactive
         /// </summary>
         public static void SendMessageToAll(this GameObject gameobject, string methodName, params object[] parameters) {
             MonoBehaviour[] components = gameobject.GetComponentsInChildren<MonoBehaviour> (true);
             foreach (MonoBehaviour m in components) {
                 m.InvokeIfExists(methodName, parameters);
             }
         }
         /// <summary>
         /// Invoke the method if it exists in any component of the component's game object and its children, even if they are inactive
         /// </summary>
         public static void SendMessageToAll(this Component component, string methodName, params object[] parameters) {
             component.gameObject.SendMessageToAll (methodName, parameters);
         }
         
         /// <summary>
         /// Invoke the method if it exists in any component of the game object and its ancestors, even if they are inactive
         /// </summary>
         public static void SendMessageUpwardsToAll(this GameObject gameobject, string methodName, params object[] parameters) {
             Transform tranform = gameobject.transform;
             while (tranform != null) {
                 tranform.gameObject.BroadcastToAll(methodName, parameters);
                 tranform = tranform.parent;
             }
         }
         /// <summary>
         /// Invoke the method if it exists in any component of the component's game object and its ancestors, even if they are inactive
         /// </summary>
         public static void SendMessageUpwardsToAll(this Component component, string methodName, params object[] parameters) {
             component.gameObject.SendMessageUpwardsToAll (methodName, parameters);
         }
     }
 }
 

Then, provided that I have using MessengerExtensions; included at the top of my file, I can call BroadcastToAll, SendMessageToAll and SendMessageUpwardsToAll on game objects and components. They work as their sibling methods, but should trigger inactive components and objects.

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Jan 08, 2016 at 11:56 AM 0
Share

That works great for me, thanks!

avatar image aphenine · Aug 18, 2016 at 01:22 AM 0
Share

I tried running this. It didn't work at all and I don't know why. One invocation just crashed Unity. It's a really cool idea and it would have been awesome if it had worked.

avatar image genieker · Nov 15, 2016 at 10:52 AM 0
Share

Great! Thanks

avatar image
0

Answer by aphenine · Aug 18, 2016 at 02:05 PM

Another way to do this is:

https://unity3d.com/learn/tutorials/topics/scripting/events-creating-simple-messaging-system

Because you register to use an event, it's bulkier and has more code, but it works beautifully even when the item is disabled.

Unfortunately, you can't send any arguments in (although there are templated versions of UnityEvents, so it probably could be modified).

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

13 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

Related Questions

Access Script from inactive game object 0 Answers

Calling a function from a different script on the same object 1 Answer

Is it possible to send in custom objects defined in the Browser using SendMessage(), or are we limited to strings? 2 Answers

i want to fix an Object place in front of the camera 1 Answer

GetInstanceId range of possible values 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