OnBecomeInvisible in an if-statement,OnBecomeInvisible in if-statement

Hello,
I’m a complete beginner in C# and I’m trying to make an OnBecameInvisible function only work when my objects y-axis < 95.

But I’m getting an error on my OnBecameInvisible, anyone knows why and how to fix it?

Here’s my script:

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

public class LoseGame : MonoBehaviour {

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (transform.position.y > 95)
        {
            void OnBecameInvisible()
            {
                SceneManager.LoadScene(7);
            }
        }

    }
}

Thanks for the help.

@viktor_bill What causes the error you’re getting is that void OnBecameInvisible() cannot be called inside void Update(). This is because OnBecameInvisible() is the same as Update() and Start(). Update() is called every frame, Start() at the beginning and OnBecameInvisible() is called when the renderer is no longer visible by any camera.
To fix your problem you should move void OnBecameInvisible() outside of void Update() and check the y position inside of OnBecameInvisible() and then change the scene.

You are attempting to write a method inside a method. This is not valid C#. You can fix it by doing one of the following:

If you really only care about your GameObject being above a certain height, then remove your OnBecameInvisible method and the associated curly braces, and your code would work (assuming there is a scene with index 7 in your build settings).

OR

Move your OnBecameInvisible method outside of Update, and put your if statement inside it, and your code will work (again assuming there is a scene with index 7 in your build settings).