Can't set currentWaypoint integer with Random.Range

Hello I am working on a script that increments the currentWaypoint every time the player collides with a way point object. What I need to do now is set the currentWaypoint to random when the player reaches way point object 2. The problem is that because the variable currentWaypoint is not an array I cannot directly change it to a random value nor can I assign it to the waypoints variable because the waypoints variable is a transform type and not an integer. Here is my script.

var waypoints : Transform[];
var waypoint : Transform;
var speed : int;
var currentWaypoint : int;
var agent : NavMeshAgent;


function Update () {
waypoint = waypoints[currentWaypoint]; // sets the active waypoint to the next currentwaypoint
agent.SetDestination(waypoint.position); // go to the first waypoint
}

function OnTriggerEnter (other : Collider) {
if (other.tag == "Waypoint" && waypoint == 2){
waypoint = waypoints[Random.Range(0, waypoints.Length)];** //set currentWaypoint to random
}
else if  (other.tag == "Waypoint"){
currentWaypoint++;
}
}

[21436-random+waypoint.png|21436]

currentWaypoint = Random.Range(…)

If anybody stumbles upon this thread then here is the source code.`var waypoints : Transform;
var waypoint : Transform;
var speed : int;
var currentWaypoint : int;
var agent : NavMeshAgent;
var isMoving : boolean = false;
var customer : GameObject;

function Update () {
waypoint = waypoints[currentWaypoint];
if (isMoving == true){ // sets the active waypoint to the next currentwaypoint
agent.SetDestination(waypoint.position);
}
}

function OnTriggerEnter (other : Collider) {
if (other.tag == “Waypoint”){
currentWaypoint++;
}
if (currentWaypoint == 5){
Destroy(gameObject);
}
if (other.tag == “Waypoint” && currentWaypoint == 2){
currentWaypoint = Random.Range(2, 4);
}
}

function WaitAtPoint(){
isMoving = false;
yield WaitForSeconds(5);
}`

This will go to the way point objects that you set up in the scene and then select a random way point when you want it to. Make sure your map has a navigation map and make sure that the player you want to move has a navigation agent. I hope this helped someone like me and Have Fun! :smiley:

var waypoints : Transform;
var waypoint : Transform;
var speed : int;
var currentWaypoint : int;
var agent : NavMeshAgent;
var isMoving : boolean = false;
var customer : GameObject;

function Update () {
waypoint = waypoints[currentWaypoint];
if (isMoving == true){ // sets the active waypoint to the next currentwaypoint
agent.SetDestination(waypoint.position);
}
}

function OnTriggerEnter (other : Collider) {
if (other.tag == "Waypoint"){
currentWaypoint++;
}
if (currentWaypoint == 5){
	Destroy(gameObject);
	}
if (other.tag == "Waypoint" && currentWaypoint == 2){
currentWaypoint = Random.Range(2, 4);
	}
}

function WaitAtPoint(){
isMoving = false;
yield WaitForSeconds(5);
}

This is the source code, hopefully it will help someone like me.