How i do i fix error cs1519 unexpected symbol ')' and '('
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
private Rigidbody rb;
// Use this for initialization
void Start () {
{
if (GetComponent<Rigidbody>() != null)
{
rb = GetComponent<Rigidbody>();
}
else
{
Debug.LogWarning("There is no Rigidbody attached to the current GameObject. Please attach one. Attaching a temporary one now.");
rb = AddComponent<Rigidbody>();
}
}
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
}
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
please format ALL of your code and include the complete error message (including line number info)
Answer by Gobaz · May 29, 2016 at 02:19 PM
Im not sure why you get that specific error but:
But Change this: AddComponent<Rigidbody>();
To this: gameObject.AddComponent<Rigidbody>();
And this code:
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement* speed);
Is not inside a function. Also moveHorizontal, moveVertical and speed dont exist in ur code.
Answer by Mmmpies · May 29, 2016 at 02:28 PM
Your Vector3 and rb.AddForce lines near the end are outside of a function. I assume you want them in FixedUpdate. When you fix that you'll need to add a variable (float I assume) for speed as you're referencing it but it doesn't exist and also put the word gameObject before the AddComponent bit or it won't work.
Now honestly it took a few seconds to spot these things once you align all the code properly. Unity comes with Monodevelop which aligns everything for you so it's not like you have to code in notepad.
It really, really makes coding a lot easier if you're careful. And as @gif says include the line numbers for errors when you post.
EDIT - Ninja'd :D