Raycast trigger multiple components at the same time

Hi all,

Ran into another problem today. So, to start with, I had a huge custom editor script, cluttered, looked messy, not easy to understand. So I went to try a different approach. I have an enum which basically states what type of horror event this object is. Door rotation, audio play, etc. On my object, I have two scripts of the same type (horrortrigger) with one being set to Audio in the enum, and the other set to door rotation. Problem is, only one of them triggers and the other is totally ignored.

            if(hit.transform.gameObject.GetComponent<HorrorTrigger>() == true)
            {
                if (hit.transform.gameObject.GetComponent<HorrorTrigger>().b_activated == false)
                {
                    if(hit.transform.gameObject.GetComponent<HorrorTrigger>().m_horrorType == HorrorTrigger.horrorTypes.Audio)
                    {
                        hit.transform.gameObject.GetComponent<HorrorTrigger>().CallAudioHorrorEvent();
                        hit.transform.gameObject.GetComponent<HorrorTrigger>().b_activated = true;
                    }

                    if (hit.transform.gameObject.GetComponent<HorrorTrigger>().m_horrorType == HorrorTrigger.horrorTypes.DoorOpen)
                    {
                        StartCoroutine(hit.transform.gameObject.GetComponent<HorrorTrigger>().CreekOpenDoor());
                        hit.transform.gameObject.GetComponent<HorrorTrigger>().b_activated = true;
                    }
                }

            } else
            {
                return;
            }

This piece of code is inside the Raycast check, to determine if the object being looked at has a HorrorTrigger component.

So, essentially, how do i get two components of the same type to run but before different actions? Is there a way? Or should I carry on trying to write a really long editor? :slight_smile:

P.S. The b_activated boolean variable is to stop functions from running constantly (because the calls are being made inside of the update function)

instead of using

hit.transform.gameObject.GetComponent<HorrorTrigger>()

everytime, store a reference in a variable

HorrorTrigger ht = hit.transform.gameObject.GetComponent<HorrorTrigger>();

then use the reference

if(ht.b_activated == false) { //...

Hey, thanks for the reply but still no joy. Only one of the horror triggers is still being called :frowning:

I’ve posted an image to further explain what I need :slight_smile:

You can use your existing system by changing GetComponent to GetCompoments and then iterating through all the returned components.

Or you can use ExecuteEvents, which does the same thing for you. :wink:

Oh, I’ve never heard of ExecuteEvents before. Will take a look into it :slight_smile: thanks for the help! I’ll let you know how it goes :smile:

            if(hit.transform.gameObject.GetComponent<HorrorTrigger>() == true)
            {

                HorrorTrigger[] ht = hit.transform.gameObject.GetComponents<HorrorTrigger>();

                for(int i = 0; i < ht.Length; i++)
                {
                    if (ht[i].b_activated == false)
                    {
                        if (ht[i].m_horrorType == HorrorTrigger.horrorTypes.Audio)
                        {
                            ht[i].CallAutioHorrorEvent();
                            ht[i].b_activated = true;
                        }

                        if (ht[i].m_horrorType == HorrorTrigger.horrorTypes.DoorOpen)
                        {
                            StartCoroutine(ht[i].CreekOpenDoor());
                            ht[i].b_activated = true;
                        }
                    }
                }



            } else
            {
                return;
            }

… I can’t believe it was that simple…well, now I know! thanks :smile:

It was going so well…till the audio clips decided to stop running correctly…

Basically, one clip after the other should play. But, they do not. You know what’s strange? If i attach two horror event scripts to my door, and make them both of type ‘Audio’ I get no problems at all. Yet, when one horror trigger is ‘Audio’ and the other ‘Door open’ it messes up. Please ignore the comments, I’ve not got round to changing them xD My code is posted below…these are the functions I am calling. Again, if one object has two horror scripts and BOTH are set to type ‘Audio’ no problems. Yet, one object, two horror scripts, one of type Audio and the other Door Open, the Audio horror type works fine but the Door Open refuses to play at all. The if statement in ‘CreakOpenDoor’ does return true. So unity says its playing…but its not.

    /*
     *
     * Summary: OnTriggerEnter is called to determine if the player has entered a Box Collider that is setup within the scene.
     * This handles some of, if not the majority of the 'Jump Scare' events. Once the player has entered the OnTriggerEnter()
     * it is imperitive that is destroyed afterwards to prevent the same event happening twice.
     *
     * Parameters: Collider other - The object that the horror trigger collides with to perform a certain action
     *
     * */
    public IEnumerator CallAudioHorrorEvent()
    {
        yield return new WaitForSeconds(m_playMusicDelay);
        // If this horror event requires a loop, Invoke the method.
        if (m_requireAudioLoop) {
               
               // Call an invoke repeating, and only repeat when the clip has finished playing
               InvokeRepeating("RequiredLoop", 0, m_audioClip.length);
               Debug.Log("Message from HorrorTrigger on Gameobject " + this.gameObject.name + ": Repeating audio called");
                       
            } else {

                // However, if we do not need a looped audio clip, simply play the audio once
                audioSource.PlayOneShot(m_audioClip);
                // Call the method to destroy the audio clip after it has finished playing
                StartCoroutine(DestroyAudioClip(m_audioClip.length));
        }
    }

    public IEnumerator CreekOpenDoor()
    {

        yield return new WaitForSeconds(m_doorOpenDelay);
        Debug.Log("Calling CreakDoor Open");
        beginDoorRotation = true;
        audioSource.clip = m_doorCreakAudio;
       audioSource.Play();
        if(audioSource.isPlaying && audioSource.clip.name == m_doorCreakAudio.name)
        {
            Debug.Break();
        }
        Debug.Log(m_doorCreakAudio.loadState);
    }