Hi.
I’m trying to run a code for making an object follow a target location. When the object reaches the target location, a new target is generated and the object follows again.
My code worked smoothly for 5 minutes, then suddenly stopped working. I haven’t made a change in the code. The problem is, I have given a condition in if statement. When the condition is false, the codes inside the else should run. But in spite of the condition being false, the else statement is not being executed.
I’ve restarted my PC, cleared the GI cache, reversed the if and else statement codes, nothing happens. The problem even persists after build.
Any idea on why this is happening?
public Rigidbody2D rb;
bool alive = true;
Vector2 targetPosition = new Vector2(6f, 3.5f);
float[,] sampleValue = { { 1.3f, 3f, -2.0f, 0.3f, 3.1f, -1.2f, 1f, -0.7f, -1.9f, 0.8f },
{ 2.1f, 0.5f, 1.9f, -1.5f, 3f, -0.2f, 2.8f, 0f, -2.0f, 1.3f},
{ 1.2f, -0.6f, 2.7f, -1f, 1f, 3.3f, -0.3f, -1.5f, 1.7f, -0.7f},
{ -0.3f, 1f, -1.9f, 0.4f, 1.6f, -0.9f, 3.1f, 1.4f, -0.5f, 2.3f} };
int i, j = 0;
public void Start()
{
i = Random.Range(0, 3);
}
void FixedUpdate()
{
if (alive)
{
Debug.Log("rb.position" + rb.position);
Debug.Log("targetPosition" + targetPosition);
if (rb.position != targetPosition)
{
Debug.Log("Inside if");
Vector2 newPosition = Vector2.Lerp(rb.position, targetPosition, 0.015f);
rb.position = newPosition;
}
else
{
Debug.Log("Inside else");
if (j < 10)
{
targetPosition.y = sampleValue[i, j];
j++;
}
else
j = 0;
}
}
}
The Debug output:
The error is in line number 22.
I just have no idea, why it suddenly just stopped working. Any help would be greatly appreciated.