Creating a short extra Script for Cheating

Edit: I misinterpreted the challenge. I thought he meant being able to collide with objects, not “if you make contact nothing happens”. So, this is only a solution for collision deactivation, skipping the level works as intended.

Basically my solution the task about making some “Cheating”-Buttons for the game. I found it odd to not make a new script for that, since it’s not really related to the “CollisionHandler” or “Movement” classes.

I cheated myself a little bit (notice the irony) and found out how to access LoadNextLevel() from my “CollisionHandler” script. It therefore is just the same as winning the level, but it gets activated on Input.GetKey and not OnCollisionEnter. For this to work, the LoadNextLevel() method needs to be public. I’ll add the codeblock under the sript as an extra for anyone wondering, but it’s the same as in the video and would work fine if you copy it into the cheating script and just call the method normally.
Just replace GetComponent().LoadNextLevel(); with LoadNextLevel();

I added some Debug.Log statements to have response in the console for activating/deactivating the BoxCollider.

using UnityEngine;

public class Cheating : MonoBehaviour
{
    private BoxCollider boxCollider;

    void Start() 
    {
        boxCollider = GetComponent<BoxCollider>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.L))
        {
            GetComponent<CollisionHandler>().LoadNextLevel();
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            HandleBoxCollider();
        }
    }

// -------------------------------------------------------------------------------------

    private void HandleBoxCollider()
    {
        if (boxCollider.enabled == true)
        {
            boxCollider.enabled = false;
            Debug.Log("God Mode activated!");
        }
        else
        {
            boxCollider.enabled = true;
            Debug.Log("God Mode Deactivated!");
        }
    }
}

Here the extra block for LoadNextLevel():

    public void LoadNextLevel()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }

Privacy & Terms