Referencing 2 functions from one script into another

I have a script to make objects fade (Script A):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FadeObject : MonoBehaviour
{
    private bool fadeOut, fadeIn;
    public float fadeSpeed;

 //   [SerializeField] private ShowerTest Shower;

  //  public bool ShowerOn;

    public void Update()
    {
        if (fadeOut)
        {
            Color objectColor = this.GetComponent<Renderer>().material.color;
            float fadeAmount = objectColor.a - (fadeSpeed * Time.deltaTime);

            objectColor = new Color(objectColor.r, objectColor.g, objectColor.b, fadeAmount);
            this.GetComponent<Renderer>().material.color = objectColor;

            if (objectColor.a <= 0)
            { 
                fadeOut = false;
            }
        }
    }
    public void FadeOutObject()
    { 
        fadeOut = true;
    }
}

And a code where I need it to work from (Script B):

private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision);
    Wet.Play();
}
private void OnCollisionExit2D(Collision2D collision)
{
    Wet.Stop();
}

I’ve tried referencing just the public void FadeOutObject() but I think I need to reference the Update method as well to make it work. I’m not sure how to reference both at once, or if I’m going about this all wrong and should be doing something else to get the Script A items to fade when collision happens on Script B.

Any help would be appreciated,

Thank you.

The Update() function on the object with the FadeObject script will be run every frame by Unity. You don’t need to call it manually.

To call a public method from another object, you need a reference to the object that has the method. If you know the object that has the script when you are building the scene, then you can use a [SerializedField] or public variable.

In your original post, it seems like you want to call FadeOutObject on the target of the collision in OnCollisionEnter2D, is this correct? In that case, you will need to call GetComponent on the collision object. GetComponent searches for the specified component on a given GameObject and then returns a reference to that component if it is found.

private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision);
    Wet.Play();
    FadeObject otherObjFade = collision.gameObject.GetComponent<FadeObject>();
    if(otherObjFade != null)
    {
        otherObjFade.FadeOutObject();
    }
}

I do not see what you already tried to reference things. You need to reference a (n instanced) class, and then call the component on that class

I have tried referencing it in the script here

public class ShowerTest : MonoBehaviour
{
    [SerializeField] private FadeObject Fade;

....

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log(collision);
        Wet.Play();
        GetComponent<FadeObject>();
    }
}

But this doesn’t work, so I didn’t know what else to do

To reference the script, use

public FadeObject fadeObjRef;

And in the inspector, drag the GameObject that has the FadeObject" script on it.

Then u can reference public methods or variables from the reference like this:

eg. Let’s get the float Speed from our reference

fadeObjRef.FadeSpeed;

Will only work if the object to be picked from the reference is public

I’ve referenced the script like you suggested, but when I try to reference the rest like you’ve stated I just get the red error lines with the errors
The name fadeObjRef does not exist in the current context.
and The name FadeSpeed does not exist in the current context.

I still don’t get how I can get the whole Update() and FadeOutObject() sections to play within my other script.