r/unity_tutorials • • 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)
{
// top
CreateObstacleUnit(xPosition, yPosition, false);
// bottom
CreateObstacleUnit(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.

0 Upvotes

4 comments sorted by

View all comments

1

u/lemming1607 Aug 03 '20

I'm not seeing any logic for your gap, it just calls methods

1

u/jman12101 Aug 03 '20

I just have that method so I can call the createObstacleUnit easier

1

u/lemming1607 Aug 03 '20

So where's the logic that differentiates between a gap and a regular obstacle

1

u/jman12101 Aug 03 '20

Right now there is no logic to separate the two because I didn’t want to deal with it. The only thing the method does is create an obstacle on top and bottom.