A lot of Unexpected errors and a keyword error

    void fire() {
        Vector3 offset = new Vector3(0, 1, 0);
        gameobject beam = instantiate(projectile, transform.position + offset, quaternion.identity) as gameobject;
        beam.getcomponent<rigidbody2d>().velocity = new vector3(0, projectilespeed, 0);
    }

Assets/Scripts/PlayerController.cs(61,9): error CS1547: Keyword `void’ cannot be used in this context

Assets/Scripts/PlayerController.cs(61,17): error CS1525: Unexpected symbol (', expecting,’, ;', or=’

Assets/Scripts/PlayerController.cs(62,44): error CS1525: Unexpected symbol 1', expecting,’, ;', or=’

Assets/Scripts/PlayerController.cs(62,47): error CS1525: Unexpected symbol 0', expecting,’, ;', or=’

Assets/Scripts/PlayerController.cs(86,0): error CS1525: Unexpected symbol `}’


The shoot function was working fine yesterday and all of a sudden these popped up. Is there any reason why they would have via script issue or is this a Unity issue?


// Full Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerController : MonoBehaviour {

    public GameObject projectile;
    public float speed = 10f;
    public float padding = 0.55f;
    public float projectileSpeed;
    public float firingRate = 0.2f;
    public float health = 200;

    float xmin;
    float xmax;
    float ymin;
    float ymax;

    MoveDirection m_MoveDirection;
    Vector3 m_ResetVector;
    Vector3 m_UpVector;
    Vector3 m_RightVector;

    void Start() {
        float distance = transform.position.z - Camera.main.transform.position.z;
        Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distance));
        Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
        Vector3 upmost = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distance));
        Vector3 downmost = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distance));
        xmin = leftmost.x + padding;
        xmax = rightmost.x - padding;
        ymin = downmost.y + padding;
        ymax = upmost.y - padding;
    }

    void Update() {
    
        if (Input.GetKey(KeyCode.LeftArrow)) {
            transform.position += new Vector3(-speed * Time.deltaTime, 0, 0);
        }
        else if (Input.GetKey(KeyCode.RightArrow)) {
            transform.position += new Vector3(speed * Time.deltaTime, 0, 0);
        }

        if (Input.GetKeyDown(KeyCode.Space)) {
            InvokeRepeating("fire", 0.000001f, firingRate);
        }
        if (Input.GetKeyUp(KeyCode.Space)) {
            CancelInvoke("fire");
        }

        float newX = Mathf.Clamp(transform.position.x, xmin, xmax);
        transform.position = new Vector3(newX, transform.position.y, transform.position.z);
        float newY = Mathf.Clamp(transform.position.y, ymin, ymax);
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);


        void fire() {
            Vector3 offset = new Vector3(0, 1, 0);
            gameobject beam = instantiate(projectile, transform.position + offset, quaternion.identity) as gameobject;
            beam.getcomponent<rigidbody2d>().velocity = new vector3(0, projectilespeed, 0);
        }

        void OnTriggerEnter2D(Collider2D collider) {
            Projectile missile = GetComponent<Collider>().gameObject.GetComponent<Projectile>();
            if (missile) {
                Debug.Log("Enemy hit by laser");
                health -= missile.getDamage();
               missile.Hit();
                if (health <= 0) {
                    Die();
                }
            }
        }

        void die() {
            LevelManager lose = GameObject.Find("LevelManager").GetComponent<LevelManager>();
            lose.LoadScene("Win Screen");
            GameObject.Destroy(gameObject);

        }   
    }
}

Hi Kyle,

Your fire, OnTriggerEnter2D and die functions are all declared inside the Update method. You need to close the Update method with a curly bracket before declaring them.

1 Like

Seb you are an angel. Thank you.

For more of an understanding, why would I want those outside of the Update Function?

As far as I know the version of C# used by Unity doesn’t support local functions (functions declared inside of other functions). In any case, local functions can only be called from the function inside which they are declared, so they have a limited use.
So to keep things simple, keep functions separate from one another, so they are all independent and able to be called from anywhere :wink:

1 Like

Thank you angel. <3

You’re very welcome :slight_smile:

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