Woah please help everything was working fine

imageimage

Then I start getting all of these errors.

I do not know what to do. Copy +pasting from the lecture gives similar errors.

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour {
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float MainThrust = 100f;
    Rigidbody rigidBody;
    AudioSource audioSource;
    enum State { Alive, Dying, Transcending }
    State state = State.Alive;
    // Use this for initialization
    void Start() {
        rigidBody = GetComponent<Rigidbody>();
        audioSource = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update() {
        //stop sound on death
        if (state == State.Alive) {
            Thrust();
            Rotate(); }
    }
    void OnCollisionEnter(Collision collision)
    {
        if (state != State.Alive) { return; }

        {
            switch (collision.gameObject.tag)

            case "Friendly"://do nothing
                print("Ok"); //todo remove
                break;
            case "Finish":
                state = State.Transcending;
                Invoke("LoadNextScene", 1f); //parameterize time
                break;
            default:
                state = State.Dying;
                print("Hit something deadly");
                Invoke("LoadFirstLevel", 1f);
                //kill player
                break;
            }
        }

        private void LoadFirstLevel()
        {
            SceneManager.LoadScene(0);
        }

        private void LoadNextScene()
        {
            SceneManager.LoadScene(1);
        }

        private void Thrust()
        {

            if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
            {
                rigidBody.AddRelativeForce(Vector3.up * MainThrust);
                if (!audioSource.isPlaying)
                {
                    audioSource.Play(); //so it doesn't layer
                } // same as if (audioSource.isPlaying == false)

            }
            else
            {
                audioSource.Stop();
            }
        }
        private void Rotate()
        {
            rigidBody.freezeRotation = true; // take manual control of rotation



            float rotationthisframe = rcsThrust * Time.deltaTime;
            if (Input.GetKey(KeyCode.A)) {

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

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

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

        }


    } }

It looks to me like you need to manage your brackets better. They are like punctuation marks to the compiler, and as such are just as important to keep track of as anything else we feed it. Below I’ve pasted your code, but changed the way the Start() and Update() methods are laid-out by putting linked brackets into the same column.

If you do the same thing to your OnCollisionEnter() method I think you’ll find you’ve got too many brackets.

using UnityEngine;
using UnityEngine.SceneManagement;

public class Rocket : MonoBehaviour 
{
    [SerializeField] float rcsThrust = 100f;
    [SerializeField] float MainThrust = 100f;
    Rigidbody rigidBody;
    AudioSource audioSource;
    enum State { Alive, Dying, Transcending }
    State state = State.Alive;

    // Use this for initialization
    void Start() 
    {
        rigidBody = GetComponent();
        audioSource = GetComponent();
    }

   // Update is called once per frame
   void Update() 
   {
       //stop sound on death
       if (state == State.Alive)
       {
          Thrust();
          Rotate(); 
       }
   }
   
  void OnCollisionEnter(Collision collision)
  {
      if (state != State.Alive) { return; }
    {
        switch (collision.gameObject.tag)

        case "Friendly"://do nothing
            print("Ok"); //todo remove
            break;
        case "Finish":
            state = State.Transcending;
            Invoke("LoadNextScene", 1f); //parameterize time
            break;
        default:
            state = State.Dying;
            print("Hit something deadly");
            Invoke("LoadFirstLevel", 1f);
            //kill player
            break;
        }
    }

    private void LoadFirstLevel()
    {
        SceneManager.LoadScene(0);
    }

    private void LoadNextScene()
    {
        SceneManager.LoadScene(1);
    }

    private void Thrust()
    {

        if (Input.GetKey(KeyCode.Space)) //can thrust while rotating
        {
            rigidBody.AddRelativeForce(Vector3.up * MainThrust);
            if (!audioSource.isPlaying)
            {
                audioSource.Play(); //so it doesn't layer
            } // same as if (audioSource.isPlaying == false)

        }
        else
        {
            audioSource.Stop();
        }
    }
    private void Rotate()
    {
        rigidBody.freezeRotation = true; // take manual control of rotation



        float rotationthisframe = rcsThrust * Time.deltaTime;
        if (Input.GetKey(KeyCode.A)) {

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

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

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

    }


} 
}
1 Like

So the above fixes the code, because I am getting errors and redlines underneath words that worked fine before like Rotate???

You have a missing { character (opening curly brace) in your switch statement within the OnCollisionEnter method, and an extra one outside of the switch statement.

The compiler assumes the rest of your methods are actually part of the code for this method, as such, your Rotate method effectively doesn’t exist.


Correction;

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

//  {								// remove this brace
	switch (collision.gameObject.tag)
	{								// you have a missing brace here
		case "Friendly"://do nothing
			print("Ok"); //todo remove
			break;
		case "Finish":
			state = State.Transcending;
			Invoke("LoadNextScene", 1f); //parameterize time
			break;
		default:
			state = State.Dying;
			print("Hit something deadly");
			Invoke("LoadFirstLevel", 1f);
			//kill player
			break;
	}
}

You could, of course, just move the brace that is above the switch statement to underneath it.

I’ve not checked the rest of your code, but try resolving the issues here first and then see if the error moves down further into your code, if not, the issue was only here.

1 Like

Okay that worked thanks guys. No idea how I managed to mess that up. I am just hoping to get started on argon assault before the new year.

I still need to learn blender then make my first game. You all recommend learning programming first right or does it matter?

1 Like

Glad to hear the problem is now resolved.

Jack’s comments regarding laying out your brackets / braces consistenly and in more block form will definitely be beneficial when you are first starting out, it does make it a lot easier to spot if one is missing. If you are using Visual Studio, you would be able to select one bracket/brace and the corresponding one will be highlighted, this also helps you identify if any are missing, or if you have too many.

Regarding modelling or coding first, I don’t think it really makes a lot of difference, which ever you prefer.

You may end up with a project with some great code but stock models, or, a stockpile of great models with no code - once you’ve done both - the world is your oyster as they say :slight_smile:

Privacy & Terms