AI wandering script?

I am trying to make a script to make an object wander around. So far I have this:

var speed : int= 5;
var wayPoint : Vector2;

function Start()
{
    InvokeRepeating("Wander", 2, 20);
}

function Wander()
{
    var wayPoint : Vector2= Random.insideUnitCircle *47;
    Debug.Log(wayPoint);
}

function Update() 
{
    transform.LookAt(wayPoint);
    rigidbody.AddRelativeForce(Vector3.forward * speed);
}

This makes the object move to the position I want but my problem is that since it's a vector2 it operates between the x and y axis. I want my object to move 2 dimensionally (between the x and z axis). When I leave the var wayPoint undefined when the game first starts the entire script doesn't work. Why is this? additionally I tried to make a while statement and then replaced it with an if statement saying that while(or if) the object was not dead that it would "Wander". Any help is appreciated. Should the function Update() be a Fixed Update instead?

FixedUpdate should be reserved for physics related functions.

You need to make wayPoint a global variable by declaring it outside of the function, put it at the top of your script, leaving it undefined obviously means that variable has no meaning and wont compile if you tried to use it in a function later e.g.

var wayPoint : Vector3;

function Start(){
   Wander();
}

You can also use Radom.insideUnitSphere instead and then set the y variable to whatever height your floor is e.g.

function Wander(){
   wayPoint = Random.insideUnitSphere*47;
   wayPoint.y = 0;
}

Instead of using invoke you could simply call Wander after you've reached the waypoint and the character is still alive. e.g.

var isAlive : boolean = true;

function Update(){
   if((transform.position - wayPoint).magnitude < 0.5 && isAlive){
      Wander();
   }
}

EDIT:

Here's your working code...

var Speed= 10;
var wayPoint : Vector3;

function Start(){
   //initialise the target way point
   Wander();
}

function Update() 
{
   // this is called every frame
   // do move code here
   transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    if((transform.position - wayPoint).magnitude < 3)
    {
        // when the distance between us and the target is less than 3
        // create a new way point target
        Wander();

        // don't need this 

        //transform.LookAt(wayPoint);
        //transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    }
}

function Wander()
{ 
   // does nothing except pick a new destination to go to
    wayPoint= Random.insideUnitSphere *47;
    wayPoint.y = 1;
   // don't need to change direction every frame seeing as you walk in a straight line only
    transform.LookAt(wayPoint);
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}

Here’s an update to this, I was noticing it always defaulted to 0,0,0 and then picked waypoints from there. This will cause it to wander aimlessly from it’s origin.

var Speed= 20;
var wayPoint : Vector3;
var Range= 10;

function Start(){
   //initialise the target way point
   Wander();
}

function Update() 
{
   // this is called every frame
   // do move code here
   transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    if((transform.position - wayPoint).magnitude < 3)
    {
        // when the distance between us and the target is less than 3
        // create a new way point target
        Wander();


    }
}

function Wander()
{ 
   // does nothing except pick a new destination to go to
    
    wayPoint=  Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range));
    wayPoint.y = 1;
   // don't need to change direction every frame seeing as you walk in a straight line only
    transform.LookAt(wayPoint);
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}
var Speed= 10;
var wayPoint : Vector3;

function Wander()
{
    wayPoint= Random.insideUnitSphere *47;
    wayPoint.y = 1;
    Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
}

function Update() 
{
    if((transform.position - wayPoint).magnitude < 3)
    {
        Wander();
        transform.LookAt(wayPoint);
        transform.position += transform.TransformDirection(Vector3.forward)*Speed*Time.deltaTime;
    }
}

**This is for anyone who wants the c# version **

using UnityEngine;
using System.Collections;

public class Wander : MonoBehaviour
{
    [SerializeField]
    float Speed = 20;

    Vector3 wayPoint;

    [SerializeField]
    int Range = 10;

    void Start()
    {
        //initialise the target way point
        wander();
    }

    void Update()
    {
        // this is called every frame
        // do move code here
        transform.position += transform.TransformDirection(Vector3.forward) * Speed * Time.deltaTime;
        if ((transform.position - wayPoint).magnitude < 3)
        {
            // when the distance between us and the target is less than 3
            // create a new way point target
            wander();


        }
    }

    void wander()
    {
        // does nothing except pick a new destination to go to

        wayPoint = new Vector3(Random.Range(transform.position.x - Range, transform.position.x + Range), 1, Random.Range(transform.position.z - Range, transform.position.z + Range));
        wayPoint.y = 1;
        // don't need to change direction every frame seeing as you walk in a straight line only
        transform.LookAt(wayPoint);
        Debug.Log(wayPoint + " and " + (transform.position - wayPoint).magnitude);
    }
}

Or instead of all that from above you could simply do this?

Vector3 pos  = Random.insideUnitSphere * wanderRadius;
nma.SetDestination(pos);

With a bit of adjustment you can make it filter out returned values that aren’t on the NavMesh. I use this because Sampling a position on the NM returns either 0,0,0 or Infinite,Infinite,Infinite for me.