new to coding and unity please help

hi all

i have bought the unity 3x game essentials book and started tutorials,
im stuck on coding as it keep producing errors this is the code the book gives me for c #.

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

// Use this for initialization
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 2f;

// Update is called once per frame
void Update () {
transform.Translate( h, v, 0);
float h = Input.GetAxis(“Horizontal”) * Time.deltaTime * moveSpeed;
float v = Input.GetAxis(“Vertical”) * Time.deltaTime * moveSpeed;
}
}

the error is Assets/Shooter.cs(14,38): error CS0841: A local variable `h’ cannot be used before it is declared

please help i tried lots of different things and cant do it ?

You want to move this:

transform.Translate( h, v, 0);

To the bottom of the Update() function, after those other two statements.

Do what GroZZleR said so that your code ends up like this :

using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

// Use this for initialization
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 2f;


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

FYI, The reason you have to do this is because you are telling it to move before you set the position you want it to move to on the x & y axis. By moving the line GroZZier said you will get the direction you want it to move & then tell it to move.

Thank you so much :slight_smile: I’m so happy :slight_smile:

That’s not the problem. The problem is he’s trying to use the variables h&v which are declared after he tries to use them.

Which is the same as saying he’s telling it to move to those coordinates before he says what the coordinates are.

void Update ()
{

float h = 0;
float v = 0;
transform.Translate( h, v, 0);
h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
}
void Update () {
transform.Translate( h, v, 0);
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
}
}

First is a bug, second is a compiler error :). That’s what I mean. The initial problem was a compiler error I know what you meant I just thought you missed that fact that it wouldn’t even compile

No worries . I thought he’d understand that reason but also thought I’d try to use normal words to explain the bit about why he had to work out the coordinates first before telling it to move. I’ve learnt the hard way that the order of doing code is important (the number of times I’ve destroyed an object then tried to do something with it…)

thanks again for the help the book is a little confusing just says
Place the given line within the Update() function in your script; - meaning after the opening curly brace of Update(){ and before the function closes with a right curly brace }. Note that this does not differ between languages: transform.Translate( h, v, 0);

Unity 3.x Game Development Essentials

also it asked me to use assets which i dont have in unity 5 like character controllers , sky boxes , terrain assets , water basic which throws me a little ?

lol i got more woes i have hit a problem again lol recieving a Parsing error

8using UnityEngine;
using System.Collections;

public class Shooter : MonoBehaviour {

    // Use this for initialization
    public Rigidbody bullet;
    public float power = 1500f;
    public float moveSpeed = 2f;

   
    // Update is called once per frame
    void Update  () {
    float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
    float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
    transform.Translate( h, v, 0);
    if(Input.GetButtonUp ("Fire1"))
    Rigidbody instance = instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
    Rigidbody.AddForce(Vector3.forward);
    instance.AddForce(fwd*power); {
    }

remove that { brace at the end of the line. Usually (depending on your code editor) if you double click on the error it takes you to it in the code. Also the error will display some numbers which will refer to the line of your code, a big help in tracking these things down!

you also need a { between line 17 and 18 and a } between 20 and 21…

oh and take out line 19

and capital i in Instantiate