ConstantForce not changing?

I have an object onto which by script this script is loaded.

I can see the value change once but it doesn’t change again. It’s like constantForce is very slow to update? or doesn’t accept updates? tried constantForce.force and constantForce.relativeForce both don’t seem to accept changes? Also tried this.gameObject.constantForce.etc? tried it also in update and fixedupdate AND ALSO i tried it with and without “new”

I can see the debugs firing but the values don’t change.

#pragma strict

var FUloops : int = 0;

function Update()
{
	if (FUloops == 1000)
	{
		FUloops = 0;
	}
	FUloops++;
		
	if (FUloops < 500)
	{	
		constantForce.relativeForce = new Vector3(0,0,-10);
		Debug.Log("less than");
	}
	else
	{
		constantForce.relativeForce = new Vector3(0,0,10);
		Debug.Log("not less than");
	}
}

I hope I’m missing something dumb???

Yes, there are a number of things wrong.

Firstly, you should never use int in update to compare or add delay (i’m assuming you did that to add delay). The reason is that Update is called on every frame so if you have fps as 100, it will be a different delay than a device on which you have fps 400 per say.

You can use coroutines or Time.deltatime to make sure that the delay is frame rate independent.

Secondly, your code changes constant force however, once the object is falling, it has a lot of force. So you need to cancel that force before adding force in the opposite direction.

Try this code: It is tested and works.

#pragma strict
var timeSum:float=0;
var currentDirection:int=0;

function addConstantForce(direction: int)
{
		gameObject.rigidbody.velocity = Vector3.zero;
 		constantForce.relativeForce = new Vector3(0,direction*10,0);
}


function Update()
{

    if (timeSum >= 2f)
    {
        timeSum = 0f;
    }
    
    timeSum+=Time.deltaTime;
 
    if (timeSum < 1f)
    {
    	
        if(currentDirection != -1)
        	addConstantForce(-1);
        	
        currentDirection = -1;
    }
    else
    {
		 if(currentDirection != 1)
		      addConstantForce(1);
		        	
		 currentDirection = 1;
    }
    

}