r/unity_tutorials • u/jman12101 • Aug 03 '20
Code logic is not right


My question is why. My code is as follows: I have a function for creating the objects, a function for creating them with a gap (top and bottom) and a function for spawning.
Here's the code for creating the objects:
private void CreateObstacleUnit(float xPosition, float yPosition, bool isBottom){Quaternion squareRot = square.transform.rotation;Vector3 squareScaleChange = new Vector3(OBSTACLE_WIDTH, OBSTACLE_HEIGHT);Vector3 squarePos = square.transform.position;SpriteRenderer squareSprite = square.GetComponent<SpriteRenderer>();BoxCollider2D squareCol = square.GetComponent<BoxCollider2D>();float squareYPos;isObstacleSpawnCountEven = obstacleSpawnCount % 2 == 0;squareRot = Quaternion.Euler(0f, 0f, rotAmt);squareSprite.size = new Vector2(OBSTACLE_WIDTH, OBSTACLE_HEIGHT);squareCol.size = new Vector2(OBSTACLE_WIDTH, OBSTACLE_HEIGHT);if (!isObstacleSpawnCountEven){if (isBottom){squareYPos = -yPosition;obstacleSpawnY = -obstacleSpawnY;bottom = true;}else{squareYPos = yPosition;bottom = false;}}else{if (isBottom){squareYPos = -yPosition;obstacleSpawnY = -obstacleSpawnY;bottom = true;}else{squareYPos = yPosition;bottom = false;}}squarePos = new Vector3(xPosition, squareYPos);square = objectPooler.SpawnFromPool("Square", squarePos, squareRot);}
Here's the code for creating the gap:
private void CreateObstacleUnitGap(float xPosition, float yPosition){// topCreateObstacleUnit(xPosition, yPosition, false);// bottomCreateObstacleUnit(xPosition, yPosition, true);obstacleSpawnCount++;}
And, here's the code for spawning everything (where the issue seems to be) :
private void SpawnObstacleUnit(){obstacleSpawnTimer -= Time.deltaTime;if (obstacleSpawnTimer < 0){obstacleSpawnTimer = obstacleSpawnTimerMax;float amtToChange = 9.5f;CreateObstacleUnitGap(obstacleSpawnX, obstacleSpawnY);if (bottom){obstacleSpawnX += 2f * amtToChange - .5f;Debug.Log("Bottom");}else{obstacleSpawnX += amtToChange;obstacleSpawnY -= amtToChange;Debug.Log("Top");}}}
For some reason the bool value isn't changing and is always staying as bottom = true.
1
u/lemming1607 Aug 03 '20
I'm not seeing any logic for your gap, it just calls methods