# The 'speed' value of the Nav Mesh Agent component does not seem to affect speed at all (Unity3D/C#)

The speed value of the Nav Mesh Agent component doesn't seem to have any effect whatsoever on the speed of my game object. Actually none of the motion values seem to have any effect. I've attached photos of how I've set up my Nav Mesh Surface Plane and how I've set up the Nav Mesh Component attached to my game object. I followed an online tutorial to use the Nav Mesh Agent component via a C# script. The object moves randomly just fine except that the speed and other values on the Nav Mesh Agent component don't seem to do anything. You will notice that there is a commented out 'randSpeed' public float variable that I experimented with. Changing that variable also did not affect the speed.

public class  RandomMovement2: MonoBehaviour
{
UnityEngine.AI.NavMeshAgent navMeshAgent;
public float timerForNewPath;
private bool inCoroutine;
//public float randSpeed;

// Start is called before the first frame update
void Start()
{
navMeshAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
//randSpeed = navMeshAgent.speed;
}

Vector3 getNewRandomPosition()
{
float x = Random.Range(-40, 40);
float z = Random.Range(-40, 40);

Vector3 pos = new Vector3(x, 0, z);
return pos;
}

IEnumerator doSomething()
{
inCoroutine = true;
yield return new WaitForSeconds(timerForNewPath);
GetNewPath();
inCoroutine = false;
}

void GetNewPath()
{
navMeshAgent.SetDestination(getNewRandomPosition());
}

void Update()
{
if (!inCoroutine && (Reaction2.stopMoving==false))
{
StartCoroutine(doSomething());
}
}
}


You're problem is that your try to change a value-type object and expect to change something.

It's the same when you want to change the position. See this example:

// this wouldn't work
Vector3 position = gameObject.transform.position;
position = new Vector3(position.x + 1, position.y, position.z);

// this would work
Vector3 position = gameObject.transform.position;
position = new Vector3(position.x + 1, position.y, position.z);
gameObject.transform.position = position;


This is a kind of basic mistake but pretty common and lightly overseen.

Also you can change the speed value on a NavMeshAgent easily in the inspector and see the agent move faster. Just try it yourself.

Nevermind. I solved it.

The problem was the order in which I assigned the speed to the Nav Mesh Agent component in code. (The code that is commented out in the code above)

I had it written as randSpeed = navMeshAgent.speed;

It should have been navMeshAgent.speed = randSpeed;

Also, since it is in the Start function, make sure that you've set the speed before you hit start on the game. You could also put it in the Update function and then you can adjust it whenever you want during the game. And of course there are other ways in code you could configure it to update the speed during the game.

Although I still don't understand why we can't change the speed and other motion settings from directly within the Nav Mesh Agent component UI.