0

I'm making a Game Called UnityCraft and I tried making a way to switch blocks!
Here is my Code:

using UnityEngine;
using System.Collections;

public class BuildScript : MonoBehaviour {

    RaycastHit hit;

    public int blockSelected = 1;

    public Transform prefab;

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetButtonDown(1)){
            blockSelected = 1;
        }

        if(Input.GetButtonDown(2)){
            blockSelected = 2;
        }

        if(blockSelected == 1){
            prefab = dirt;
        }

        if(blockSelected == 2){
            prefab = brick;
        }


        Ray ray = camera.ViewportPointToRay (new Vector3 (0.5f, 0.5f, 0));
        Vector3 G = new Vector3 (Mathf.Round (hit.point.x), Mathf.Ceil (hit.point.y), Mathf.Round (hit.point.z));

        if (Physics.Raycast (ray, out hit)) {
            if (Input.GetMouseButtonDown (0)) {
                Destroy (hit.collider.gameObject);
                print ("Block Destroyed!");
            }

            if (Input.GetMouseButtonDown (1)) {
                Instantiate (prefab, G, Quaternion.identity);
            }
        }
    }
}

I have a prefab called brick and one called dirt, and they are linked to blocks.

3
  • It comes up with parsing error Commented Jan 28, 2014 at 20:27
  • 3
    What is your question? Commented Jan 28, 2014 at 20:28
  • In what line is the error? What is the error message? Commented Jan 28, 2014 at 20:30

1 Answer 1

2

I'm assuming that the problem you're referring to is in the line

if(Input.GetButtonDown(1)){

That won't work, because GetButtonDown does not have an integer argument. It takes a string, which you can find or define in the input manager.

From your code I do take it that you want to simply use number keys? In that case, don't use the GetButton calls, but use GetKey instead. So change your code to something like

if(Input.GetKeyDown(KeyCode.Keypad1)){

for the case where pressing 1 should trigger something.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.