- Home /
I'm having problems with bools resetting within a function
Hi,
I'm quite new to programming and I might have overestimated my abilities. I'm trying to build a small Game with a few Tiles moving along a grid made out by Waypoints. Long story short, I'm trying to call a function which declares the exact position and moves the tile accordingly.
My Problem each frame the bool (Moving) telling the tile to move is reset and nothing happens...
void IStepsRight(int Steps, GameObject Tile, bool Moving, bool HalfwayMoved, bool StartSet, Vector3 Start, Vector3 Ziel, Vector3 Zwischenschritt)
{
//bool Moving = new bool();
//bool HalfwayMoved = new bool();
//bool StartSet = new bool();
//Vector3 Start = new Vector3();
//Vector3 Ziel = new Vector3();
//Vector3 Zwischenschritt = new Vector3();
Debug.Log(Moving);
Debug.Log(Steps);
if (Tile.gameObject.transform.position.x == IA.transform.position.x && StartSet == false) { Debug.Log("IA"); Start = IA.transform.position; StartSet = true; }
else if (Tile.gameObject.transform.position.x == IB.transform.position.x && StartSet == false) { Debug.Log("IB"); Start = IB.transform.position; StartSet = true; }
else if (Tile.gameObject.transform.position.x == IC.transform.position.x && StartSet == false) { Debug.Log("IC"); Start = IC.transform.position; StartSet = true; }
else if (Tile.gameObject.transform.position.x == ID.transform.position.x && StartSet == false) { Debug.Log("ID"); Start = ID.transform.position; StartSet = true; }
if (Steps == 1 && Start != null && Moving == false && StartSet)
{
Debug.Log("Steps == 1 && Start != null && Moving == false");
if (Start == IA.transform.position) { Ziel = IB.transform.position; Zwischenschritt = IA_B.transform.position; Moving = true;}
else if (Start == IB.transform.position) { Ziel = IC.transform.position; Zwischenschritt = IB_C.transform.position; Moving = true;}
else if (Start == IC.transform.position) { Ziel = ID.transform.position; Zwischenschritt = IC_D.transform.position; Moving = true;}
else if (Start == ID.transform.position) { Ziel = IA.transform.position; Zwischenschritt = ID_A.transform.position; Moving = true;}
}
else if (Steps == 2 && Start != null && Moving == false && StartSet)
{
Debug.Log("Steps == 2 && Start != null && Moving == false");
if (Start == IA.transform.position) { Ziel = IB.transform.position; Zwischenschritt = IA_B.transform.position; Moving = true;}
else if (Start == IB.transform.position) { Ziel = IC.transform.position; Zwischenschritt = IB_C.transform.position; Moving = true;}
else if (Start == IC.transform.position) { Ziel = ID.transform.position; Zwischenschritt = IC_D.transform.position; Moving = true;}
else if (Start == ID.transform.position) { Ziel = IA.transform.position; Zwischenschritt = ID_A.transform.position; Moving = true;}
}
else if (Steps == 3 && Start != null && Moving == false && StartSet)
{
Debug.Log("Steps == 3 && Start != null && Moving == false");
if (Start == IA.transform.position) { Ziel = IB.transform.position; Zwischenschritt = IA_B.transform.position; Moving = true;}
else if (Start == IB.transform.position) { Ziel = IC.transform.position; Zwischenschritt = IB_C.transform.position; Moving = true;}
else if (Start == IC.transform.position) { Ziel = ID.transform.position; Zwischenschritt = IC_D.transform.position; Moving = true;}
else if (Start == ID.transform.position) { Ziel = IA.transform.position; Zwischenschritt = ID_A.transform.position; Moving = true;}
}
if (Moving == true)
{
Debug.Log("Moving!!!");
Debug.Log("Start " + Start);
Debug.Log("Zwischenschritt " + Zwischenschritt);
Debug.Log("Ziel " + Ziel);
Debug.Log("Position " + Tile.gameObject.transform.position);
Tile.gameObject.transform.position = Vector3.MoveTowards(Tile.gameObject.transform.position, Zwischenschritt, Speed * Time.deltaTime);
Tile.gameObject.transform.rotation = Quaternion.RotateTowards(Tile.gameObject.transform.rotation, Quaternion.Euler(0f, 0f, 180f), Speed * Time.deltaTime);
if (Tile.gameObject.transform.position == Zwischenschritt)
{
Debug.Log("Halfway through!");
HalfwayMoved = true;
}
if (HalfwayMoved)
{
Tile.gameObject.transform.position = Vector3.MoveTowards(Tile.gameObject.transform.position, Ziel, Speed * Time.deltaTime);
Tile.gameObject.transform.rotation = Quaternion.RotateTowards(Tile.gameObject.transform.rotation, Quaternion.Euler(0f, 0f, 180f), Speed * Time.deltaTime);
}
if (Tile.gameObject.transform.position == Ziel) { HalfwayMoved = false; Moving = false; StartSet = false; Steps -= 1; Debug.Log("Guess I'm done"); }
}
if (Moving == false) {Debug.Log ("Not Moving!"); }
else if (Steps == 0 && Moving == false)
{
I.GetComponentInChildren<Clickable>().GotClicked = false;
}
}
I tried creating the variables directly in the code or feeding them in as parameters. Both lead to the same problem. this function will be called in multiple instances so I can't just use predefined variables.
I'm thankful for any input because I've been stuck for hours. Thanks!
Your answer
Follow this Question
Related Questions
Check if a function is no longer being called? 3 Answers
Call void for one Object 1 Answer
How to make on/off-like gui-button? 3 Answers
if Application.LoadLevel Error 1 Answer
Moderators please DELETE long titles 0 Answers