Sending Messages to other Game Objects

I’ve got the following Hierarchy and need my LevelLoader to receive the notification about the player death sent by my CollisionProtocol on the PlayerShip which is parented to the PlayerRig.

I’ve looked at the various methods for sending messages in the GameObject class and determined they won’t work. Is there another option I’m missing or do I need to use the IActor pattern Sam teaches in the RPG course?

See My Project code

Why wouldn’t you just call a message from the load level script instead?

Probably because I’ve not been introduced to that concept before, or if I have I don’t recognize it. I will certainly research it in the morning though.

Updated Fri Aug 16 2019 09:00
So after a bit of research this morning, I’m a bit more confused than when I started. It seems like you are asking “Why don’t I use the IActor class?”. But my research led me to the EventExecute class and then to the ScriptablObject class and then Component.SendMessage which is the same as GameObject.SendMessage.

As the documentation on the EventExecute class is rather cryptic, it is a ways beyond my understanding and coding abilities at the moment and thus only muddied the waters as it were. This, of course, leaves me right back where I started, “Is there a simple way to get messages between Game Objects without them having to know about one another? Or, do I need to bite the bullet and go for the IActor class from the RPG Course?”

Gonna go ahead and mark this topic even though I never got it figured out as there just doesn’t seem to be a way to do what I was trying to do.

The SendMessage stuff is supposed to be used for prototyping only. The Unity developers say that themselves in one of their articles about performance optimisation.

If you want to send messages, you could try to implement an event system.


@Marc_Carlyon, do you know anything about the IActor? Could it be helpful for @Capricas_Kirito?

Not really @Nina i think your advice on delegates and events is what needs to be used here which we do cover later in the RPG course.
IActor or what i think Capricas meant was IAction which is an interface class we use to cancel the action in the action scheduler.

I did mean the IAction interface, sorry for the confusion. I ended up deciding against the interface while figuring it out and came up with the following for the time being:

using UnityEngine;
using UnityEngine.SceneManagement;

namespace Core
{
    public class SceneLoader : MonoBehaviour
    {
        [Tooltip("in seconds")] [SerializeField] float loadDelay = 1.5f;
        Health player;
        float timeSinceLoad = Mathf.Infinity;

        private void Awake()
        {
            int sceneLoaders = FindObjectsOfType<SceneLoader>().Length;
            if (sceneLoaders > 1) { Destroy(gameObject); }
            else { DontDestroyOnLoad(gameObject); }
        }

        void Start()
        {
            Scene scene = SceneManager.GetActiveScene();
            if (scene.buildIndex == 0) { Invoke("LoadMenu", loadDelay); }
            else { ResetPlayer(); }
        }

        void Update()
        {
            if (player == null) { ResetPlayer(); }
            IncrimentTimers();
            if (NeedReload())
            {
                OnPlayerDeath();
            }
        }

        private void ResetPlayer()
        {
            player = GameObject.FindWithTag("Player").GetComponent<Health>();
        }

        private bool NeedReload()
        {
            if (player == null) { ResetPlayer(); }
            if (timeSinceLoad >= loadDelay)
            {
                if (player.GetIsAlive() == false) { return true; }
            }
            return false;
        }

        private void IncrimentTimers()
        {
            timeSinceLoad += Time.deltaTime;
        }

        void OnPlayerDeath()
        {
            timeSinceLoad = 0f;
            LoadMenu();
        }

        void LoadMenu()     // Called by string ref in Start()
        {
            SceneManager.LoadScene(1);
        }
    }
}

If I decide to expand my Argon Assault project beyond where Ben and Rick left it, I’ll revisit the interface idea.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms