LoadNextLevel() method in Brick.cs is still red

In Brick.cs script, the LoadNextLevel() method is throwing an “LevelManager does not contain a definition for LoadNextLevel”. It seems like Brick.cs cannot access the method in LevelManager.

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

public int maxHits;
private int timesHit;
private LevelManager levelManager;

// Use this for initialization
void Start () {
	timesHit = 0;
	levelManager = GameObject.FindObjectOfType<LevelManager> ();
}

// Update is called once per frame
void Update () {
	
}

void OnCollisionEnter2D (Collision2D collide) {
	timesHit++;
	SimulateWin ();
}

//TODO Remove this method
void SimulateWin() {
	levelManager.LoadNextLevel ();
}

}

In your script, LevelManager - you may have either not yet created your method LoadNextLevel(), or, perhaps spelt it incorrectly, C# is case-sensitive.

Check your code for the LevelManager.cs script, have you added this method yet? If not, you can add the following stub (just an empty method);

public void LoadNextLevel()
{
    // note : I must add some code here for this to actually do anything
}

…you will find that the error message will disappear, however, as per the comment, unless you add some code within that method, nothing else will happen.

I hope this helps.


See also;

This is my LevelManager script:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

public void LoadLevel(string name) {
	Debug.Log ("Level load requested for: " + name);
	Application.LoadLevel(name);
}

public void QuitRequest() {
	Debug.Log ("I want to quit!");
	Application.Quit ();
}

public void LoadNextLevel () {
	Application.LoadLevel (Application.loadedLevel + 1);

}

You have a missing closing curly brace at the end of the script. This would cause an error and prevent the script from being built.

I fixed the curly braces but it still throws the same error

public void LoadNextLevel () {
	Application.LoadLevel (Application.loadedLevel + 1);
}

}

Hello @Fion,

Could you post the entire LevelManager script again please, and the script from which you are accessing it when the error is thrown.

Is the error exactly the same? If you are unsure, please post a screenshot of the error message from within the Unity editor.

LevelManager.cs:

using UnityEngine;
using System.Collections;

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name) {
		Debug.Log ("Level load requested for: " + name);
		Application.LoadLevel(name);
	}

	public void QuitRequest() {
		Debug.Log ("I want to quit!");
		Application.Quit ();
	}

	public void LoadNextLevel () {
		Application.LoadLevel (Application.loadedLevel + 1);
	}
}

Brick.cs

using UnityEngine;
using System.Collections;

public class Brick : MonoBehaviour {

	public int maxHits;
	private int timesHit;
	private LevelManager levelManager;

	// Use this for initialization
	void Start () {
		timesHit = 0;
		levelManager = GameObject.FindObjectOfType<LevelManager> ();
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnCollisionEnter2D (Collision2D collide) {
		timesHit++;
		SimulateWin ();
	}

	//TODO Remove this method
	void SimulateWin() {
		levelManager.LoadNextLevel ();
	}
}

The error is the same

Can you select the LevelManager game object in the Hierarchy please and take another screenshot with the details displayed in the Inspector for me.

There’s your issue.

So, you have a LevelManager game object with the LevelManager script attached, but this script is failing to compile.

The fix is to resolve whatever the underlying issue is. From what you have posted it looks ok, are you certain that the code you posted is from the same file that you have wired up within your LevelManager game object?

If you are not certain, zip up the whole project, as long as it is under 10mb you can upload it here and I will take a look for you.

https://drive.google.com/file/d/0By2Y9KOGtkETcUlJaFRicTJkZTA/view?usp=sharing

I’m downloading.

In the meantime, try this to see if we can get around the compilation errors.

  1. comment out the levelManager.LoadNextlevel(); statement in the SimulateWin() method within Brick.cs. e.g.
	//TODO Remove this method
	void SimulateWin() {
		// levelManager.LoadNextLevel ();
	}

Assuming this is the only place where it is being called, the fact that it can’t see the yet to be compiled LevelManager class with this new method is now irrelevant.

Make sure all of your scripts are saved. Go back into Unity, click Run. Can you get the game to run without any errors at this stage?

I’ve just opened up your LevelManager.cs file from within Assets and the new method LoadNextLevel() isn’t in there.

Perhaps you saved it after adding the closing curly brace for the class, but not after adding the new method?


  • Double-click the LevelManager.cs script from within the Assets folder and add the new method;

    	public void LoadNextLevel () {
    		Application.LoadLevel (Application.loadedLevel + 1);
    	}
    
  • Save the script.

If you have commented out the line within the SimulateWin() method I mentioned above;

  • Remove the comment so that it the method can be called again

  • Save the script.

Now, go back into Unity;

  • Click Run.

All should be well.

1 Like

Yes that worked!! Thank you!!

1 Like

You’re very welcome @Fion, glad you have it resolved and can move forwards again :slight_smile:

Privacy & Terms