0
votes

EDIT : Apparently when I select Windowed, the buttons work. Why is this? Why doesn't it work without that box selected?

I created a game in Unity and everything works fine in Unity itself. When I build and run the game, my buttons no longer recognize my mouse clicks. Why is this?

This is a basic pong game. I don't know why it would work in Unity and not outside of Unity if it was the code but here is the code.

Code :

paddle script:

using UnityEngine;
using System.Collections;

public class paddle : MonoBehaviour {

    public float paddleSpeed = 1;
    public Vector3 playerPos = new Vector3(0,0,0);

    // Update is called once per frame
    void Update () {
        float yPos = gameObject.transform.position.y + (Input.GetAxis ("Vertical") * paddleSpeed);
        playerPos = new Vector3 (-20,Mathf.Clamp(yPos, -13F,13F),0);
        gameObject.transform.position = playerPos;
    }
}

ball script:

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

    public float ballVelocity = 1500;

    private Rigidbody rb;
    private bool isPlay; //false by default
    int randInt; //random ball directon when game begins

    // Use this for initialization
    void Awake () {
        rb = gameObject.GetComponent<Rigidbody> ();
        randInt = Random.Range (1,3);
    }

    // Update is called once per frame
    void Update () {
        if(Input.GetMouseButton(0) == true && isPlay == false){
            transform.parent = null;
            isPlay = true;
            rb.isKinematic = false;
            if(randInt == 1){
                rb.AddForce(new Vector3(ballVelocity,ballVelocity,0));
            }
            if(randInt == 2){
                rb.AddForce(new Vector3(-ballVelocity,-ballVelocity,0));
            }
        }
    }
}

enemy script:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

    public float speed = 8;
    private Vector3 targetPos;
    private Vector3 playerPos;
    private GameObject ballObj;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        ballObj = GameObject.FindGameObjectWithTag ("ball");
        if (ballObj != null) {
            targetPos = Vector3.Lerp (gameObject.transform.position, ballObj.transform.position, Time.deltaTime * speed);
            playerPos = new Vector3 (-20, Mathf.Clamp (targetPos.y, -13F, 13F), 0);
            gameObject.transform.position = new Vector3 (20, playerPos.y, 0);
        }
    }
}

score script:

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour {



public TextMesh currScore;
    public GameObject ballPref;
    public Transform paddleObj;

    GameObject ball;
    private int score;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        ball = GameObject.FindGameObjectWithTag("ball");
        currScore.text = "" + score;
        }
    void OnTriggerEnter(Collider other) {
        if(other.tag == "ball"){
            score += 1;
            Destroy(ball);
            (Instantiate(ballPref, new Vector3(paddleObj.transform.position.x + 1, paddleObj.transform.position.y,0), Quaternion.identity) as GameObject).transform.parent = paddleObj;
        }
    }
}

title screen script:

#pragma strict

function Start () {

}

function Update () {

}

function StartGame () {
    Application.LoadLevel("Main");
}

function ExitGame () {
    Application.Quit();
}
1
Did you hard code any x,y offsets that would change when running the game outside of unity? - Ryan Mann
@Ryios I don't believe so. - Justin
Well, it's hard to say without seeing some code. No unity expert, but anyone else would have the same problem. - Ryan Mann
@Ryios I added the code. - Justin

1 Answers

0
votes

From what I am understanding, you want Unity to recognize your mouse button event even when the mouse pointer is not inside the game Window.

There could be 2 ways to achieve this:
1. The easy way, make your game full screen.
2. If you want your game in a window box, you must check "Run In Background" option in Player settings -> Resolution. And then, learn how to hook mouse event: http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C

Please note that the hooking mouse event tutorial only works on Windows, not Mac nor Linux. I do not know how to do it with Mac or Linux.