So in my game I only have one gameobject and that is a spawner. What is does is spawn an arena, player, and defensemen. So what is supposed to happen is the defensmen are supposed to follow the player when the game starts. The problem I am having is when everything spawns I can’t get the player prefab clone to become the target for the defenseman.
Here is the code for the Player:
var speed : int = 1; // Adjust to make your NPC move however fast you want.
function Start () {
GameObject.FindGameObjectsWithTag("Defenseman").SendMessage("SetPlayer");
}
function Update ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Here is the code for the Defensemen:
#pragma strict
var target: Transform; // drag the target here
var speed: float = 5.0; // object speed
private var moving = false; // object initially stopped
private var dir: Vector3;
private var plChar : GameObject;
function Start () {
}
function SetPlayer () {
target = GameObject.FindWithTag("Player").transform;
}
function Update () {
dir = target.position - transform.position; // calculate the target direction...
moving = true; // and enable movement
if (moving){ // if movement enabled...
// move the object in the calculated direction (world coordinates):
transform.Translate(dir * speed * Time.deltaTime, Space.World);
}
}
So what I thought is when the player spawns he could use .SendMessage to the defensmen to use the SetPlayer function. In the SetPlayer function in the Defensmen script it basically says find anyone with the tag Player and make that the target. Well this doesn’t work. When I start the game the defensemen just stay still and I get a few errors. They are either “UnityEngine.GameObject.SendMessage” or “Object Reference Not set to an Instance of the Object”. If anyone could help me find a way to set the player that spawns in the target for the Defesnmen that woudl be great.