so i started learning unity and first thing i got in mind that's not so hard is to make something like "brick breaker" kind of game, all went good till i started to fix some bugs. The one that i need help with is that a platform will stop moving if i got out of screen, i got idea that i need to make borders and said to script if it touch border then stop moving in that direction butt that was something i know i can do so i wanted to do something harder like this: when platform get to right x coordinates then it stop moving, and my unity crash every time i run the game. So i waned to know if that's even possible or i just lose 5 hours of my life ;D sorry for bad English and here is my code Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player1 : MonoBehaviour { public float speed; private bool border1 = true; private bool border2 = true; void Update() { try { if (border1 == false) { speed = 0; } if (border2 == false) { speed = 0; } if (Input.GetKey(KeyCode.LeftArrow)) { transform.position += Vector3.left * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.RightArrow)) { transform.position += Vector3.right * speed * Time.deltaTime; } while (transform.position.x > 9.5) { border1 = false; } while (transform.position.x < -9.5) { border2 = false; } } catch { Debug.Log("sjebo se"); } } }
While declares a loop but what you really want to do is check that condition once only. And by the looks of it you already know the right keyword for that so with that little bit of advice, I think you can already work it out by yourself.
in other words, 'while' might look similar to 'if', but in reality it is much more similar to 'for' loop. however, unlike 'for' it's a more general syntax, that doesn't use a counter. 'if' is a single, one-time condition check that evaluates the expression in the parentheses and moves on with life. 'while' is typically used as a code block that will repeat until the expression is evaluated as true false (edit: slip of the tongue), meaning it will suspend all activity, including unity moving forward in time, advancing to the next frame, and so on. if there is nothing going on with it, it will practically freeze everything in place, as it cannot resolve gracefully and move on. also it doesn't make sense to use 'try' if you don't know what it's for. OR, maybe you do, but 'while' freezing won't produce an error, at least not in a technical sense. it just hangs unity indefinitely. btw I'm a native speaker of the language in which that Debug.Log speaks
here is code that fix this problem, i just made him tp by small amount when he go out Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player1 : MonoBehaviour { public float speed; public GameObject gameObjectToMove; void Update() { if (transform.position.x <= -9.52f) { gameObjectToMove.transform.position = new Vector3(-9.45f, -4, 1); } if (transform.position.x >= 9.52f) { gameObjectToMove.transform.position = new Vector3(9.45f, -4, 1); } if (Input.GetKey(KeyCode.LeftArrow)) { transform.position += Vector3.left * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.RightArrow)) { transform.position += Vector3.right * speed * Time.deltaTime; } } }