Hello, I'm going to create a background on which objects that fall are appearing. This has been achieved, the object (currently one) this, however, after reaching the value of y (-224) should be created again, or simply to the initial position. However, this does not happen I have already tried many conditions, however, nothing gives the intended effect. I am asking for help in solving this puzzle. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class SquareBackground : MonoBehaviour { [SerializeField] private float xLimit; [SerializeField] private float[] xPosition; [SerializeField] private Wave[] wave; private float currentTime; List<float> remainingPositions = new List<float>(); private int waveIndex; float xPos = 0; int rand; void Start() { currentTime = 0; remainingPositions.AddRange(xPosition); } void Update() { if(transform.position.y == -224) { SelectWave(); fallObject(xPos); } } void fallObject(float xPos) { int r = 0; string objectName = ""; if (r == 0) objectName = "fallSquare"; GameObject fallingObject = PollingObjects.instance.GetPooledObject(objectName); fallingObject.transform.position = new Vector3(xPos, transform.position.y, 0); fallingObject.SetActive(true); } void SelectWave() { remainingPositions = new List<float>(); remainingPositions.AddRange(xPosition); waveIndex = Random.Range(0, wave.Length); currentTime = wave[waveIndex].delayTime; if (wave[waveIndex].spawnAmount == 1) xPos = Random.Range(-xLimit, xLimit); else if(wave[waveIndex].spawnAmount > 1) { rand = Random.Range(0, remainingPositions.Count); xPos = remainingPositions[rand]; remainingPositions.RemoveAt(rand); } for(int i = 0; i < wave[waveIndex].spawnAmount; i++) { fallObject(xPos); rand = Random.Range(0, remainingPositions.Count); xPos = remainingPositions[rand]; remainingPositions.RemoveAt(rand); } } } [System.Serializable] public class Wave { public float delayTime; public float spawnAmount; } The PollingObject class is responsible for copying the object. The most significant functions are "Update ()" and "fallObject (float)".
Here's the problem: Code (csharp): if(transform.position.y == -224) Position.y is a floating point value and it will never be exactly equal to -224. Use "<= -224" instead. Additionally, the logic feels overcomplicated here.
Yes, you were right. Swapping the sign helped. I added some parameter changes and the puzzle has been solved. I just have a question why the parameters x and y shown in unity are different from those in the code?
Because numbers are floating points. Not all decimal numbers can be precisely represented, and as a result most programs that display floating points round them off to nearest value, based on precision. The most likely scenario is that coordinate was -223.9999999 or something like that. Aside from using <= instead of == another option is this function: https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html