Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Gil_Parnon · Jun 18, 2015 at 09:45 PM · c#mouse movement

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";
             }
         }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Xarbrough · Jun 18, 2015 at 11:37 PM

"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 ;)

 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";
         }
     }
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Gil_Parnon · Jun 19, 2015 at 03:56 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xarbrough · Jun 19, 2015 at 04:06 PM 0
Share

Glad I could help. You should mark my answer as accepted then, to close this thread. ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Renderer on object disabled after level reload 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges