I broke my code during this section! Getting a CS1022 error!

I was going along fine and then … something happened? I was moving things around and now it’s giving me a CS1022 error. I’m sure it’s just a stupid syntax error, misplaced bracket, or something not capitalized but I can’t for the life of me figure out what’s wrong with it.

Here is the code:

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour
{
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float mainThrust = 100f;
    [SerializeField] AudioClip mainEngine;
    [SerializeField] AudioClip success;
    [SerializeField] AudioClip death;
    [SerializeField] ParticleSystem mainEngineParticles;
    [SerializeField] ParticleSystem successParticles;
    [SerializeField] ParticleSystem deathParticles;


    Rigidbody rigidBody;
    AudioSource audioSource;

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

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

    // Update is called once per frame
    void Update() {
        // to do somewhere stop sound on death
        if (state == State.Alive)
        {
            RespondToThrustInput();
            RespondToRotateInput();
        }
        
    }

    void OnCollisionEnter(Collision collision)
    {
        if (state !=State.Alive) { return; } //ignore collions when dead

        switch (collision.gameObject.tag)
            {
            case "Friendly":
                break;
            case "Finish":
                StartSuccessSequence();
                break;
            default:
                StartDeathSequence();
                break;
        }
    }

    private void StartSuccessSequence()
    {
        state = State.Transcending;
        audioSource.Stop();
        audioSource.PlayOneShot(success);
        Invoke("LoadNextLevel", 1f); // parameterise time
    }

    private void StartDeathSequence()
    {
        state = State.Dying;
        audioSource.Stop();
        audioSource.PlayOneShot(death);
        Invoke("LoadFirstLevel", 1f); // parameterise time
    }

    private void LoadNextLevel()
    {
        SceneManager.LoadScene(1); // Allow for more than 2 levels
    }
    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0); // Allow for more than 2 levels
    }

    private void RespondToThrustInput()
    {
        if (Input.GetKey(KeyCode.Space)) // Thrust while rotating
            {
            ApplyThrust();
            }
            else
            {
                audioSource.Stop();
                mainEngineParticles.Stop();
            }
        }
    }



    private void ApplyThrust()
    {
        rigidBody.AddRelativeForce(Vector3.up * mainThrust);
        if (!audioSource.isPlaying) //so the audio doesn't layer on top of itself
        {
            audioSource.PlayOneShot(mainEngine);
            mainEngineParticles.Play(mainEngineParticles);
        }

    }

    private void RespondToRotateInput()
    {
        rigidBody.freezeRotation = true; // take manual control of rotation

        float rcsThrust = 100f;
        float rotationThisFrame = rcsThrust * Time.deltaTime;

        if (Input.GetKey(KeyCode.D))
        {
            transform.Rotate(Vector3.forward * rotationThisFrame);
        }

        else if (Input.GetKey(KeyCode.A))
        {

            transform.Rotate(-Vector3.forward * rotationThisFrame);
        }

        rigidBody.freezeRotation = false; // resume physics control of rotation
    }
}

Any help would be appreciated! Thanks!

@csprinks Can you take a few screenshots so i can see what the error looks like your code looks fine i don’t see any errors in the code

Lloyd Risper

Thanks but I actually found it! There was a rogue extra bracket in there somehow. I had to go through the finished sample code and check it against mine for the section meticulously line by line to find it, and the debug doesn’t help because where it’s saying the error is isn’t where the sneaky bracket was at all. :sleepy::rage::rage::face_with_symbols_over_mouth::face_with_symbols_over_mouth:

1 Like

@csprinks Good job.

Lloyd Risper

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

Privacy & Terms