Home > Mobile >  Unity is asking for a } but I don't see why
Unity is asking for a } but I don't see why

Time:11-04

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

public class playerMovement : MonoBehaviour
{

    public int speed;

    // Start is called before the first frame update
    void Start()
    {
        public float translation = Input.GetAxis("Horizontal") * speed;

        translation *= Time.deltaTime;
    }

    // Update is called once per frame
    void Update()
    {

        transform.Translate(translation, 0, 0);

    }
}

Line 12 is giving an error for a missing closing curly bracket however I can't figure out why. I've tried google and friends however I still couldn't find a soloution to the issue and it's really annoying me.

CodePudding user response:

It's confused by the public in

public float translation = Input.GetAxis("Horizontal") * speed;

which shouldn't be there. Change it to

float translation = Input.GetAxis("Horizontal") * speed;

instead.

EDIT: based on the comments it seems you're simply looking for

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

public class playerMovement : MonoBehaviour
{

    public int speed;

    // Update is called once per frame
    void Update()
    {
        float translation = Input.GetAxis("Horizontal") * speed;
        translation *= Time.deltaTime;
        transform.Translate(translation, 0, 0);
    }
}

instead. There is no point in looking at translation in the Start method.

CodePudding user response:

Move variable outside the function.

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

public class playerMovement : MonoBehaviour
{

    public int speed;
    public float translation;

    // Start is called before the first frame update
    void Start()
    {
        translation = Input.GetAxis("Horizontal") * speed;
        translation *= Time.deltaTime;
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(translation, 0, 0);
    }
}
  • Related