Section 5 Lecture 83

I’m not getting understand what this error is exactly about… :confused: . I corrected that warning error. but do i need to create a new script ?
@Rob

Hello @Sabeen_Moin,

SceneManager provides a method named LoadScene. The LoadScene provides two signatures. A method signature is simply the way in which it can be used. In the case of LoadScene you can either pass in a string as the name of the scene to load, or, you can pass in an integer which represents the scene index in the build settings.

In your screen shot above you are not passing in either of the two options mentioned above, you are passing in the same statement again and a +1 on the end of it.

You do wanted to increment the number that is the scene index, but you will need to ascertain what the current scene’s build index is first, before you can increment it, or, use something else which returns the current scenes build index as an integer, and then +1 that.

For example;

public void LoadNextLevel()
{
    Scene currentScene;
    int nextSceneIndex;

    // retrieves the current scene details
    currentScene = SceneManager.GetActiveScene();

    // retrieves the current scene's build index and increments it
    nextSceneIndex = currentScene.buildIndex +1;

    // loads the next scene based on the incremented scene index
    SceneManager.LoadScene(nextSceneIndex);
}

Note, this may not be the most efficient way, I have simply provided a way that should work. I have also written this in long hand to make it easier to read. Further reading below :slight_smile:


Updated Wed May 10 2017 14:43

Updated to include response for duplicate / related post.

Your first error message is due to using the old Application.LoadLevel method, this is no longer available in newer versions of Unity. Replace Application.LoadLevel with SceneManager.LoadScene, e.g.;

public void LoadLevel(string name) {
    Debug.Log("Level Load Requested: " + name);
    SceneManager.LoadScene(name);
}

Note: If you use SceneManager then you need to implement the correct using directive at the top of the script, e.g;

using UnityEngine.SceneManagement;

See also;

Unity Scripting API : SceneManager.LoadScene
Unity Scripting API : ScreneManager.GetActiveScene
Unity Scripting API : Scene.buildIndex

2 Likes

Thank you :slight_smile: i fixed that script error but still the level is not changing and the brick is not smashing when i hit ball :confused: .

Hello @Sabeen_Moin,

Could you zip the project up and upload it, I will happily take a look for you.

1 Like

Block Breaker.zip (3.7 MB)

Here is my project “Block Breaker” .

Hi @Sabeen_Moin,

Just a very quick glance, but there is a typo in your following method;

	void OnCollisionEnder2D (Collision2D col) {
		timesHits++;
		SimulateWin();
	}

It should be called OnCollisionEnter2D - as such, nothing happens when there is a collision as you don’t have a method to handle it on the Brick.

1 Like

Thank you i fixed it but there is now another error about build settings

All scenes are there already in Build Settings.:sweat:

All of your playable scenes are in the build settings yes, however, somewhere you have entered the name of a scene to load as “OtherSceneLoad”.

It could be wired up to a button if you have a menu in place, in which case look at the OnClick settings for the button component(s).

It could be wired up in code, in which case perform a search across the entire solution for “OtherSceneLoad”.

In either case, replace it with whatever should be there.

HINT: Take a look in your LevelManager.cs

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

public class LevelManager : MonoBehaviour {

	public void LoadLevel(string name){
		Debug.Log("Level Load Requested: "+name);
		SceneManager.LoadScene(name); 
	}

	public void QuitRequested(){
		Debug.Log("I Want to quit!");
	}

	public void LoadNextLevel() {
		SceneManager.LoadScene("OtherSceneName",LoadSceneMode.Additive);
	}
}

This is my Level Manager script. I don’t know what to do and how to fix it.

Hello @Sabeen_Moin,

This was covered in my first reply to you in this topic.

If you look at your LoadNextLevel() method, you are trying to load a scene called “OtherSceneName”, this is why you are seeing the error message because it is trying to load a scene with that name, which doesn’t exist.

Referring to my original reply, SceneManager.LoadScene can either be used with a string - e.g. “The_Name_Of_Your_Scene”, or, an int, being the build index of the scene you wish to load.

You already have a method which provides loading scenes by name, it’s the one called LoadLevel(), thus, this one just needs to load scenes by the build index.

What you need to do is calculate the build index of the currently loaded level, then, add one to it. My first reply in this topic gives an example that you could use.

I’m doing as much as i can, i have 0% knowledge about coding sorry for i keep asking questions…

Hello @Sabeen_Moin, it really isn’t a problem :slight_smile:

Also, my apologies, I made a typo myself in that example;

    // retrieves the current scene details
    currentScene = SceneManager.GetCurrentScene();

should be;

    // retrieves the current scene details
    currentScene = SceneManager.GetActiveScene();

(I have amended my original response to reflect this)

1 Like

Thank you so much!! :smiley: now i can able to smash bricks and get onto next level :innocent:

1 Like

You’re more than welcome.

The important thing here, is that going forward you are able to understand these methods and see what they are doing and how they are working. You may have other scenarios where you need to do some debugging and work things through. :slight_smile:

1 Like

Yes now I’m getting understand how code works :slight_smile: . I can never learn like this in college.

Microsoft Virtual Academy has a really good free course on C# fundamentals. I did that first before starting this. I think it helped quite a bit.

1 Like

Cheers Rob, this helped me get unstuck. I went one step further to your solution and chained both methods together:

SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
1 Like

You’re more than welcome Simon, happy to help :slight_smile:

1 Like

Privacy & Terms