Levels not loading

After carefully scrutinizing my code and comparing to the project changes, I think I must ask, why my code won’t work. The trouble is, after I hit an obstacle and state is set to dead, the LoadNextLevel() method seems to just forget it is it’s turn and never loads. If someone can be so kind and look at it then I’d be eternally grateful.

using UnityEngine;
using UnityEngine.SceneManagement;

public class rocket : MonoBehaviour {
    Rigidbody rigidBody;
    AudioSource rocketRumble;

    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;

    enum State {alive, dead, ascending};
    State state = State.alive;

    // Use this for initialization
    void Start () {
        rigidBody = GetComponent<Rigidbody>();
        rocketRumble = GetComponent<AudioSource>();
    }
	
	// Update is called once per frame
	void Update () {
        if (state == State.alive)
        {
            Rotate();
            Thrust();
        }
        
        
	}

    private void OnCollisionEnter(Collision collision)
    {
        if (state != State.alive) { return; }
        

        switch (collision.gameObject.tag)
        {
           
            case "Friendly":
                break;
            case "finish":
                state = State.ascending;
                Invoke("LoadNextScene()",1f);
                break;
            default:
                state = State.dead;
                
                Invoke("PlayerDied()", 1f);
                break;
        }
    }

    private void PlayerDied()
    {
        SceneManager.LoadScene(0);
    }

    private void LoadNextScene()
    {
        SceneManager.LoadScene(1);
    }

    private void Rotate()

        
    {
        rigidBody.freezeRotation = true;

        float rotationThisFrame = rcsThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.A))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }
        rigidBody.freezeRotation = false;
    }

    private void Thrust()
    {
        if (Input.GetKey(KeyCode.Space))
        {
            
            rigidBody.AddRelativeForce(Vector3.up * mainThrust);
            if (!rocketRumble.isPlaying)
            {
                rocketRumble.Play();
            }
        }
        else
        {


            rocketRumble.Stop();

        }
    }

Hi,

In your two Invoke method calls, remove the brackets from the name of the method you want to actually invoke, e.g.

Invoke("LoadNextScene", 1f);

instead of

Invoke("LoadNextScene()", 1f);

Hope this helps :slight_smile:


See also;

2 Likes

Oh… thank you. I knew it would be something like this :slight_smile:

1 Like

All sorted now?

Yep. That fixed it. Thanks again.

1 Like

No problem. :slight_smile:

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

Privacy & Terms