Hi,
I am trying to write a simple wandering AI script. Basically I have decided to make a box the target of my character, and when he reaches the box, the box will pick a new random position on the map within a set area. Here is my script:
using UnityEngine;
using System.Collections;
public class SelfSpawnning : MonoBehaviour {
private int posX; // positions for waypoint object
private int posY;
private int posZ;
public Vector3 pos;
public Transform waypoint; // box that the character will seek
public bool reachedWaypoint; // whether charater reached the waypoint
// if reached it, randomize new position for waypoint
void Awake(){
reachedWaypoint = false;
SeedPos(); // seeds initial position for waypoint
waypoint.position = pos;
Instantiate(waypoint);
}
// Use this for initialization
void Start () {
reachedWaypoint = false;
}
// Update is called once per frame
void Update () {
if (reachedWaypoint) // do we need to find a new pos for the waypoint?
SeedPos(); // if so we seed new position
}
void SeedPos(){
posX = Random.Range(-25, 25);
posY = 5;
posZ = Random.Range(-25, 25);
pos = new Vector3(posX, posY, posZ);
waypoint.transform.position = new Vector3(posX, posY, posZ);
reachedWaypoint=false;
}
}
It runs error free (all you need to do is drag some transform into the public var area for “waypoint”. However, I want the position to change each time you make the reachedWaypoint variable true. The Vector3 pos generates new coordinates by calling SeedPos(); but the position of the waypoint doesnt change. If I try to go waypoint.position.x = posX, I get an error, saying I can’t modify that value for some reason.
How can I go about making my object move and what is the reason for that error? Thanks for any input, much appreciated.