- Home /
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++;
}
}
Check the tag on gameObject Waypoint #2. If it's properly tagged, add
debug.log("Saw " + other.tag);
or something to validate you are getting the OnTriggerEnter event you are expecting.
When currentWaypoint is 4 and it hits the ++, bad things will happen.
I have no problem moving the customer from one waypoint to another with a simple increment. Also I was planning on telling unity what to do after 4, later. Right now the problem is that the currentWaypoint variable wont randomize. If I put waypoint = waypoints[Random.Range(0, waypoints.Length)];
into the update function then the only thing that randomizes is the variable "waypoint" but it has no effect on the the "currentWaypoint" variable thus the customer will not move unless the "currentWaypoint" variable is changed.
You lost me there. I don't understand a few sentences in your question now. You can assign a random.range to an int.
"The problem is that because the variable currentWaypoint is not an array I cannot directly change it to a random value"
?
Sorry for the confusion. If it is possible to assign a random.range to an integer how would I do it? If if I try currentWaypoint(Random.Range(0, waypoints.Length));
Unity says that it is not possible to invoke an expression of type 'int'.
Thank you so much! You are a god hahaha. I can't believe it was so simple.
Answer by Tyrebear · Jan 31, 2014 at 08:58 AM
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! :D
Answer by Tyrebear · Jan 31, 2014 at 08:58 AM
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.