Trying to End Game on Mouse Movement

I’m making a popup game, something appears on the screen and they can’t jump. The goal is to not move the mouse. I’m trying to use Vector3s to compare the state of the mouse location so that if it changes when it updates the game ends. This is not working because it says that, “The referenced script on this Behaviour is missing!” and that “get_mousePosition can only be called from the main thread”.

I put the script on the background plane, because I figured that the script just needs to be able to tell if the mouse moved, it doesn’t matter what it’s running from.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameConditions : MonoBehaviour {

		public Text loseText;

		Vector3 movement = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z);
		// Use this for initialization
		void Start () 
		{
			loseText.text = "";
		}
		
		// Update is called once per frame
		void Update ()
		{
			if (movement != new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z))
			{
				loseText.text = "Game Over";
			}
		}
}

“References script is missing” means, that your class GameConditions can not be compiled or was renamed or something else happened, so that it got (temporarily) detached from the GameObject.

The other error comes from the fact, that you are trying to call new Vector3 (Input.mousePosition… from outside of a function, which is not possible. This line of code must go in Start or Awake if you want to set it initially. The member area of your class can only include compile time constants, which means, it cannot do any math calculations like Input.mousePosition, it can only do things like int number = 5; because 5 is a constant. Any value that changes during runtime must go in a function.

Here is some working code. In Start(), right before the game begins, the mouse position is stored in initialPosition. Then in Update() every frame checks if the current mouse position is different from the initial one and if it is, it sets the text to game over. Be aware that this last step will happen in every frame from then on, which you want to later change so that it only gets changed once. But all in all it gives you Game Over as soon as you move the mouse. Tested in the editor as well :wink:

using UnityEngine;
using UnityEngine.UI;

public class GameConditions : MonoBehaviour 
{
	public Text loseText;

	private Vector3 initialPosition;

	void Start () 
	{
		initialPosition = Input.mousePosition;
		loseText.text = "";
	}
	
	void Update ()
	{
		if (Input.mousePosition != initialPosition)
		{
			loseText.text = "Game Over";
		}
	}
}

Thanks, that worked! That was the last piece, I’ve finished my game.