Collision Debug Toggle code issue

Hello,

struggling to get my code to work after following this lecture.

This is the code:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class rocket : MonoBehaviour
{
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;
    [SerializeField] float levelLoadDelay = 2f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip onDeath;
    [SerializeField] AudioClip onSuccess;

    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem onDeathParticles;
    [SerializeField] ParticleSystem onSuccessParticles;


    Rigidbody rigidBody;
    AudioSource audioData;

    enum State {Alive, Dying, Transcending}
    State state = State.Alive;

    bool collisionsDisabled = false;

    // Start is called before the first frame update
    void Start()
    {
        rigidBody = GetComponent<Rigidbody>();
        audioData = GetComponent<AudioSource>();

    }

    // Update is called once per frame
    void Update()
    {
        if (state == State.Alive)
        {
            RespondToRotateInput();
            RespondToThrustInput();
        }
        if (Debug.isDebugBuild)
        {
            RespondtoDebugKeys();
        }    
    }

    private static void RespondtoDebugKeys()
    {
        if (Input.GetKeyDown(KeyCode.L))
        {
            SceneManager.LoadScene(+1);
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            collisionsDisabled = !collisionsDisabled;
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state !=State.Alive || collisionsDisabled) {return;}

        switch (collision.gameObject.tag)
        {
            case "Friendly":
                // Do nothing
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                onDeathParticles.Play();
                break;
        }
    }

Issue I’m having is that it isn’t picking up collisionsDisabled = !collisionsDisabled;

Error comes up saying an object reference is required but when I compare my code to what’s used in the lecture I can’t see why it wouldn’t work. Any tips?

No worries! My partner helped solve it. I simply just had the ‘static’ left in the class. Works now I took it out :smiley:

Good job. I’m wondering, though, why the static keyword was added in the first place. That happened to a couple of students in the past few weeks.

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

Privacy & Terms