- Home /
Why these variables(bool) aren't changing???
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class ballCtrl : MonoBehaviour
{
private bool doRight = false;
private bool doLeft = false;
public bool isCrashed = false;
private Rigidbody2D rb;
public float sensivity;
void Start () {
rb = GetComponent<Rigidbody2D> ();
doRight = false;
doLeft = false;
}
void Update () {
if (!doRight && !doLeft) {
Debug.Log (doRight);
Debug.Log (doLeft);
return;
} else if (doLeft) {
//Left
rb.AddForce (Vector2.left * Time.deltaTime * sensivity);
Debug.Log ("Left Go");
} else {
//Right
rb.AddForce (Vector2.right * Time.deltaTime * sensivity);
Debug.Log ("Right Go");
}
}
public void LeftDown(){
doLeft = true;
//rb.AddForce (Vector2.left * Time.deltaTime * sensivity);
Debug.Log ("Left Down");
return;
}
public void LeftUp(){
doLeft = false;
Debug.Log ("Left Up");
}
public void RightDown(){
//rb.AddForce (Vector2.right * Time.deltaTime * sensivity);
doRight = true;
Debug.Log ("Right Down");
return;
}
public void RightUp(){
doRight = false;
Debug.Log ("Right Up");
return;
}
}
These codes are not working. Actually, the code makes NO compile error, but the actor isn't moving. and also Debug.Log() is still calling!!! Log: Right up, Right down.... So, Do the variables are error? or there is something errored?!?!??
please help. I am having this problem for 3 days... Project's aren;t continuing if this can't fix.
Answer by SteenPetersen · Jul 30, 2017 at 06:08 AM
You are not calling your methods in update:
void Update () {
HandleMovement();
}
private void HandleMovement(){
if (Input.GetKey("w"))
{
// ask it to do something here including changing your bools
}
}
but...These LeftDown ( your script's Handle$$anonymous$$ovement) is called when UI clicked.
Im basically just making it so that the Handle$$anonymous$$ovement is requested in update everyframe. that way I can now change the handlemovement method and things get called in every frame.
You could for example write:
if (Input.Get$$anonymous$$ey("w"))
{
doRight = true;
}
as a test
Your answer
![](https://koobas.hobune.stream/wayback/20220612131512im_/https://answers.unity.com/themes/thub/images/avi.jpg)