- Home /
error CS1525: unexpected symbol. How do I fix this and why am I getting this error?
I'm very new to coding in general and I am using C# I'm trying to make it so when you press W the sprite (which is in a 2D space) moves up I'm getting an error CS1525 unexpected symbol "transform"
using UnityEngine;
using System.Collections;
public class PlayerHandler : MonoBehaviour
{
float speed = 1.0f;
// Initialization
void Start ()
{
}
// Update
void Update ()
{
if (Input.getkey(KeyCode.W)
transform(Vector3.forward * speed * Time.deltaTime);
}
}
you need to get the position (transform.position), modify it and then set it to the new value.
something like:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.W)
{
var newPosition = transform.position;
newPosition += (Vector3.forward * speed * Time.deltaTime);
transform.position = newPosition;
}
pay special attention to the function names too - you most likely got an error with your code.
the unity docs can usually help solve these issues...waiting for some kind soul to help you out here is going to slow you down ;)
Answer by maccabbe · Dec 26, 2015 at 05:37 PM
If you are just starting out with Unity we suggest checking out the learn section, as there exists several tutorials, documentation and live training sessions. Specifically, if you need to learn about programming then please go through the scripting tutorials.
In this case the error you got is because of the line
if (Input.getkey(KeyCode.W)
Where the second parenthesis are not closed. Your computer then continues to read the next line, expecting it to finish the true/false condition and realizes that the symbol transform is not being used as part of the conditional hence the error unexpected symbol "transform".
Then the next line also has an error as you use an object as a function when as explained above you should be adjusting the position of the transform. An alternative to setting position would be to use Transform.Translate.
So your code should be
if (Input.getkey(KeyCode.W))
transform.Translate(Vector3.forward * speed * Time.deltaTime);
Your answer
Follow this Question
Related Questions
instantiating unique classes of a type 0 Answers
I need some help with my high score.,I need help with my highsore. 1 Answer
Multiple Cars not working 1 Answer
"Only assignment, call, increment, decrement and new object expressions can be used as statements" 1 Answer
Distribute terrain in zones 3 Answers