- Home /
 
Why is "GetMouseButtonDown(0)" not working on 2 functions?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerScript : MonoBehaviour { private bool isInGame = false;
 private Rigidbody2D myRB;
 Vector2 tempPos;
 private bool isRight = false;
 private void Start()
 {
     myRB = GetComponent<Rigidbody2D>();
 }
 private void FixedUpdate()
 {
     Debug.Log(isRight);
     Debug.Log(myRB);
     if (isRight)
     {
         if (Input.GetKeyDown(KeyCode.RightArrow)) # DOESNT WORK: Input.GetMouseButtonDown(0)
         {
             tempPos = transform.position;
             tempPos.x += 2.0f;
             transform.position = tempPos;
             isRight = false;
         }
     }
     if (!isRight)
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow)) # DOESNT WORK: Input.GetMouseButtonDown(0)
         {
             tempPos = transform.position;
             tempPos.x += -2.0f;
             transform.position = tempPos;
             isRight = true;
         }
     }
     if (!isInGame)
         return;
 }
 public void InitGame()
 {
     isInGame = true;
 }
 
               }
This script is working... but not with getmousebuttondown(0) :(
Answer by unity_G2yrNlWYdpJKuw · Oct 21, 2017 at 04:53 PM
Try using void Update instead of FixedUpdate
with Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow) it works?
well maybe try Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0)
Well sorry... i said it badly... with the arrows it work ... and with Get$$anonymous$$ouse.. it works only to one side.. (it transforms but u can use (Get$$anonymous$$ouse..) the same button to go back) so after one Get$$anonymous$$ouse.. it transforms to left and than the isRight is set to true and than when i click (getmouse..) again .. nothing's happening (no transformations no isRight changes) it works only with 2 different buttons... so my question is: Why is it not working only with Get$$anonymous$$ouse... ? anyway thanks for trying me to help
 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 public class PlayerScript : $$anonymous$$onoBehaviour { 
 private bool isInGame = false;
  private Rigidbody2D myRB;
  Vector2 tempPos;
  private bool isRight = false;
  private void Start()
  {
      myRB = GetComponent<Rigidbody2D>();
  }
  private void Update()
  {
      Debug.Log(isRight);
      Debug.Log(myRB);
      if (isRight)
      {
          if (Input.Get$$anonymous$$ouseButtonDown(0))
          {
              tempPos = transform.position;
              tempPos.x += 2.0f;
              transform.position = tempPos;
              print("isRight");
              isRight = false;
          }
      }
      else if (!isRight)
      {
           if (Input.Get$$anonymous$$ouseButtonDown(0))
          {
              tempPos = transform.position;
              tempPos.x += -2.0f;
              transform.position = tempPos;
              print("!isRight");
              isRight = true;
          }
      }
      if (!isInGame)
          return;
  }
  public void InitGame()
  {
      isInGame = true;
  }
 
                    let me know if this works
 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 public class PlayerScript : $$anonymous$$onoBehaviour { 
     private bool isInGame = false;
 
     private Rigidbody2D myRB;
     Vector2 tempPos;
     private bool isRight = false;
     private void Start()
     {
         myRB = GetComponent<Rigidbody2D>();
     }
     private void Update()
     {
         Debug.Log(isRight);
         Debug.Log(myRB);
 
 
         if (Input.Get$$anonymous$$ouseButtonDown(0)&& isRight)
             {
                 tempPos = transform.position;
                 tempPos.x += 2.0f;
                 transform.position = tempPos;
                 print("isRight");
                 isRight = false;
             }
 
 
 
         else if (Input.Get$$anonymous$$ouseButtonDown(0)&& !isRight)
             {
                 tempPos = transform.position;
                 tempPos.x += -2.0f;
                 transform.position = tempPos;
                 print("!isRight");
                 isRight = true;
             }
 
         if (!isInGame)
             return;
     }
     public void InitGame()
     {
         isInGame = true;
     }
 
                  might this helps?
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Fixed force in direction of touch. 1 Answer
Distribute terrain in zones 3 Answers
Gravity Switch Touch 1 Answer
Global Fog Messy Transition Between Objects and Skybox 1 Answer