Search Unity

Space Shooter Code Issues

Discussion in 'Community Learning & Teaching' started by cinefyl, Mar 23, 2015.

Thread Status:
Not open for further replies.
  1. cinefyl

    cinefyl

    Joined:
    Mar 23, 2015
    Posts:
    2
    Hello, I am a new Unity student, and I decided to start with the space shooter project as my first. It went smoothly enough, up until part 06 - Moving the player. I wrote the code the video and website said to, and got the following errors:

    • Assets/_scripts/PlayerController.cs(24,25): error CS1525: Unexpected symbol `}', expecting `)', `,', or `;'
    • Assets/_scripts/PlayerController.cs(28,24): error CS1547: Keyword `void' cannot be used in this context
    • Assets/_scripts/PlayerController.cs(28,25): error CS1525: Unexpected symbol `(', expecting `)', `,', `;', `[', or `='
    I've got the code attached, so if anyone can tell me what's wrong and how to fix it I would really appreciate it.
     

    Attached Files:

  2. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.    [System.Serializable]
    4.    public class Boundary
    5. {
    6.    public float xMin, xMax, zMin, zMax;
    7. }
    8. public class PlayerController : MonoBehaviour
    9. {
    10.    public float speed;
    11.    public float tilt;
    12.    public Boundary boundary;
    13.    public GameObject shot;
    14.    public Transform shotSpawn;
    15.    public float fireRate;
    16.    private float nextFire;
    17.  
    18.    void Update()
    19.    {
    20.      if (Input.GetButton("Fire1") && Time.time > nextFire)
    21.        {
    22.          nextFire = Time.time + fireRate;
    23.          GameObject clone = Instantiate(shot, shotSpawn.position, shotSpawn.rotation)as GameObject
    24.        }
    25.      Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    26.    }
    27.  
    Looking at your code, line 23, the line starting "GameObject clone =" has no semicolon at the end. When the compiler gets to line 24, it's therefore confused because it expected one of those symbols it listed prior to getting to the closing curly brackets }.


    How to use code tags.
     
    cinefyl likes this.
  3. cinefyl

    cinefyl

    Joined:
    Mar 23, 2015
    Posts:
    2
    I added the missing semicolon to line 23, and that fixed it. I moved to the next video, and I added the Bolt prefab to the player gameobject, added the ShotSpawn transform, and set the firerate to .25, but now when I run the game, the ship fires a constant stream of lasers. I tried changing the mapping for Fire1, but it didn't change anything.
     
  4. Socrates

    Socrates

    Joined:
    Mar 29, 2011
    Posts:
    787
    Looking back at your code which I quoted, I missed that you have an extra Instantiate() call on line 25. This means that you are creating a bullet every single Update().

    You should only have the Instantiate() call on line 23 that is inside the block of code for the if() statement on line 20.
     
  5. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
Thread Status:
Not open for further replies.