Click here to Skip to main content
15,747,637 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
    public float horizontalInput;
    public float speed = 10f;
    public float xRange = 10f;
    public GameObject projectilePrefab;

    // Start is called before the first frame update
    void Start()
    // Update is called once per frame
    void Update()
        if (Input.GetKeyDown(Keycode.Space))
            //launch projectile from player
            Instantiate(projectilePrefab, transform.position, projectilePrefab.transform.rotation);

        if (transform.position.x < -xRange)
            transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
        if (transform.position.x > xRange)
            transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);

        horizontalInput = Input.GetAxis("Horizontal");

        transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed); 

What I have tried:

I'm new in coding, but this is based on a tutorial I follow, it supposedly should work if I press the space bar, but nothing :(. Just in case, I have a macbook air.
Updated 16-Jun-20 23:11pm
Patrice T 16-Jun-20 20:31pm    
Please, give full error message, including line number.
F-ES Sitecore 17-Jun-20 5:54am    
Keycode.Space should be KeyCode.Space, note the capital "C".

If you are new to development, then don't start playing with Unity - it brings it's own problems which make your life much harder. Just start with the basics, preferably from a C# course, or a Book - avoid YouTube tutorials like the plague: they are a very poor way to learn development.

The idea is you get the basics solid first, and learn how to create code instead of blindly typing it in an hoping it works!

There are two types of errors in development: Compile time errors (or "Compilation errors") and Runtime errors (or "Bugs")
The first version prevents your app from running at all because if you get a compilation error no EXE file is generated.
The second may or may not produce any error message, but if it does it will be after you tell you app to run.

Your problem is the first type, and will provide you with a lot of information. Not just the error message "Identifier expected", it will also give you the file name and a line number in the file. If you double click on the message in the Errors pane of Visual Studio then it will take you directly to line line that it doesn't like, and put a red squiggly line under parts of it. Those are the bits it has a problem with.
Have a good look at the line and the line above - sometimes a mistake with the previous line shows up as an error on the line after it.

See what I mean about learning the basics first?

Try that, and it should be reasonably simple to fix - but I don't think it's the code you show above; that doesn't seem to have any problems I can see that might cause an "identifier expected" error.
Share this answer
Maciej Los 17-Jun-20 5:12am    
To resolve your issue, follow the link: What is CS1041? – Unity[^]
This might be helpful too: 5 Common Coding Errors in C# and Unity and how to Solve them | Learn to Create Games[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900