Can you give provide feedback/insight on the gameplay of my game Portal Reign?

Ok, so what you know now is that the GameOver method is only being called by the CollisionHandler.cs script and that that script is only on the Player Ship. It lists the GameController because its a method within the GameController.cs script which is attached to that object.

We know we can make changes now without messing up lots of things.

So, next steps…

  • Within GameController.cs, remove this line of code;
    private bool restart = false;
    
  • Within the OnGUI method, remove these lines of code;
    if (restart)
    {
        if (Input.GetKeyDown(KeyCode.R))
        {
            Invoke("SceneManager.LoadScene(1)", 2f);
        }
        if (Input.GetButtonDown("Restart"))
            Invoke("SceneManager.LoadScene(1)", 2f);
    }
    
  • Within the SpawnWaves method, remove these lines of code;
    if (gameOver)
    {
        restartText.text = "Press 'R' for Restart";
        restart = true;
        break;
    }
    
  • Update the Update method as follows;
    void Update()
    {
        if (enemiesLeft == 0)
        {
            endGame();
        }
    
        if (gameOver)
        {
            if (Input.GetKeyDown(KeyCode.R))
            {
                Debug.Log("Restart Game");
            }
        }
    }
    
  • Within the GameOver method, add this line of code (ideally where you set the other text);
    outcome.text = "You Lose";
    restartText.text = "Press 'R' for Restart";
    
  • Select the Restart UI Text GameObject
    • Set its width to 400
    • Set the font size to 50
      image

When you run the game, crash into an enemy ship, you’ll see the “Game Over” text, the “You Lose” text and the “Restart” text, press R and you’ll see a message in the console.

Okay will do.

Let me know when you get to the above outcome and also see the message in the console.

Okay hold on.

Yes I see it when I crash into enemy.

Ok, so now let’s tackle the reloading of the scene.

You have a script called SceneLoader.cs, you are currently using it only on the Splash scene.

You can confirm this by right-clicking on the SceneLoader.cs script and select Find References In Scene. For the Game scene you’ll get none. If you save the Game scene and then open the Splash scene, do the same again, you should see it filters the Hierarchy and shows the Scene Loader GameObject.

Can you confirm please.

Hold on.

Confirmed

I fixed the alignment of the text in the splash screen.

This is coming out great I can’t wait to update the asset from Daz3D :wink:

Great.

So, we can use the SceneLoader.cs script but we need to be a little bit careful because it is also being used elsewhere.

Let’s tidy up what we have at the moment;

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    private void Start()
    {
        Invoke("LoadFirstScene", 2f);
    }

    public void LoadFirstScene()
    {
        SceneManager.LoadScene(1);
    }
}

Note, we’ve also set the LoadFirstScene method to be public so that it can be called from other classes.

  • Drag the Scene Loader GameObject from the Hierarchy into your Prefabs folder
  • Save the Splash scene
  • Open the Game scene
  • Drag the Scene Loader prefab into the Hierarchy

Let me know when you’ve done the above.

Okay

Done.

Ok, if you’ve tried running the game you’ll most likely have noticed that the ship takes off a bit and then repeats. The reason for this is the less than ideal SceneLoader.cs script, what is happening is that when your Game scene loads it calls the Start method which then calls LoadFirstScene, so effectively it’s just going around and around in circles.

This behaviour is what you want on the splash screen, because you do want it to load the first actual scene (methods are named badly here too!)…

So, let’s make a couple of changes;

using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    private int currentSceneIndex = -1;


    private void Awake()
    {
        currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    }

    private void Start()
    {
        if(currentSceneIndex == 0)
        {
            Invoke("LoadFirstScene", 2f);
        }
    }

    public void LoadFirstScene()
    {
        SceneManager.LoadScene(1);
    }
}

This is still a less than ideal solution but it builds on what you already have and will work, for now, until improved.

What it does is to set a member variable with the current scene’s build index when the Awake method is called (this happens before Start).

Then, in the Start method we check what the current scene build index is, if its zero then we’re on the Splash scene in which case we go ahead and load the first scene. Nothing happens if it isn’t.

Update the script and save the changes.

Run the game, fly around, crash.

You should see “Game Over” / “You Lose” / “Press ‘R’ to Restart”, and now when you do, the level will restart.

Okay hold on.

I get the restart method in the console

Ah, that’s because I didn’t finish writing the instructions above, my bad.

Ok, so open GameController.cs and make the following changes;

  • Add this line of code;
    private SceneLoader sceneLoader;
    
  • Within the Start method, add this line of code (at the top);
    sceneLoader = FindObjectOfType<SceneLoader>();
    
  • Within the Update method, after the Debug.Log statement, add the following line of code;
    sceneLoader.LoadFirstScene();
    
  • Save the script
  • Run the game

Note, you can delete that Debug.Log statement once you’ve know it’s working as expected.

No No I thought that at least seeing the message in the console state it works correct :slight_smile: will continue

It means that the logic is correct, e.g. you are at the game over state and we have acknowledged a key press to restart the level, but it wasn’t reloading the level - with the above, it will.

I have one more bonus item for you, once you’ve confirmed the above is all ok. After that I’m going to bed as it’s almost 2am here.

Amazing you did it !! :slight_smile:

Privacy & Terms