An object reference is required to access non-static member

I’m trying to connect the coroutine of this script from another one. So I learned that I need to change the IEnumerator Fade to public static. Now it just spawned another error. I looked for answers and I can only find get.component solutions but I think that it will not work in Fade
Here is the script. Early thank you so much for helping me. :slight_smile:

GameObject Player;
public string SceneName;
public float secBeforeFade = 3.0f;
public float fadeTime = 5.0f;
public Texture fadeTexture;
private bool fadeIn = false;
private float tempTime;
private float time = 0.0f;

void Start ()
{
	Player = GameObject.Find ("Player");
}

public static IEnumerator Fade()
{
    yield return new WaitForSeconds(secBeforeFade);
    fadeIn = true;
}

void Update()
{
    if (fadeIn)
    {
        if (time < fadeTime) time += Time.deltaTime;
        tempTime = Mathf.InverseLerp(0.0f, fadeTime, time);
    }

    if (tempTime >= 1.0f)
        SceneManager.LoadScene(SceneName);
}

void OnTriggerEnter (Collider col)
{
	
	if (col.tag == "Player")
    {
        StartCoroutine(Fade());
    }
}

void OnGUI()
{
    if (fadeIn)
    {
        Color colorT = GUI.color;
        colorT.a = tempTime;
        GUI.color = colorT;
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeTexture);
    }
}

}

You don’t have to make the function static. All you have to do is make a reference of this instance of this class and call the function from there, something like this:

public class someClass : MonoBehaviour
{
    public OtherClass otherClass; // this is the reference of the class you provide, replace the OtherClass with the class's name

    void someFunction()
    {
        StartCoroutine(otherClass.Fade());  //here you call the IEnumarator
    }
}

You need to fill the reference either you do it through the editor or through code.

Note1: make sure to remove the static keyword from your script.

Note2: the reason it doesn’t work when it is static is that it modifies an non static field (the bool fadeIn) which produces the error because the compiler does not know in which instance of the class should modify that field.

Cheers hope that helps.