- Home /
Setting a prefab clone as a variable in code
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.
Ehm... why target = GameObject.FindWithTag("Player").transform; ?
#pragma strict
var target: GameObject; // 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.FindGameObjectWithTag("Player");
}
function Update () {
dir = target.transform.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);
}
}
And your moving script... dont know, but Im think it isnt perfect way to move character. But, who knows?
So I changed the two scripts.
Player Script:
var speed : int = 1; // Adjust to make your NPC move however fast you want.
private var defense$$anonymous$$ans : GameObject;
function Start () {
defense$$anonymous$$ans = GameObject.FindGameObjectsWithTag("Defenseman");
for (var defense$$anonymous$$an : GameObject in defense$$anonymous$$ans){
var def$$anonymous$$anScr : FollowPlayer = defense$$anonymous$$an.GetComponent(FollowPlayer);
def$$anonymous$$anScr.working = true;
}
}
function Update ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
and the Defensmen Script:
#pragma strict
var target: GameObject; // 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;
public var working : boolean;
function Start () {
}
function SetPlayer () {
target = GameObject.FindGameObjectWithTag("Player");
}
function Update () {
if (working == true){
SetPlayer();
}
dir = target.transform.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);
}
}
I did have to change a few things like the active variable because I kept getting an error that it wasn't correct or something so I just changed it to working and I didn't get the error but there is a different problem. I get this error Assets/Scripts/Player$$anonymous$$oveForward.js(7,53): BCE0022: Cannot convert 'UnityEngine.GameObject[]' to 'UnityEngine.GameObject'. I don't know why and I don't know how to fix this problem but can you help me out with it. Even when I tried to fix a couple of things to get the game testing the defensmen still dont follow the player. Anyone have any ideas???
$$anonymous$$y mistake :)
private var defense$$anonymous$$ans : GameObject[];
So this seems to be working for me but it isn't actually follow my player its following like an area where the player spawns. I don't know how to get it to follow the player that actually in the game. Any idea's ???
Answer by chariot · Feb 11, 2015 at 01:08 PM
First script reworked
var speed : int = 1; // Adjust to make your NPC move however fast you want.
private vat defenseMans : GameObject;
function Start () {
defenseMans = GameObject.FindGameObjectsWithTag("Defenseman");
for (var defenseMan : GameObject in defenseMans){
var defManScr : DefenseManScriptName = defenseMan.GetComponent(DefenseManScriptName);
defManScr.active = true;
}
}
function Update ()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Second script:
#pragma strict
var target: GameObject; // 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;
public var active : boolean;
function Start () {
}
function SetPlayer () {
target = GameObject.FindGameObjectWithTag("Player");
}
function Update () {
if (active){
SetPlayer();
}
dir = target.transform.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);
}
}
if u want to turn off finding player position after first use, just turn off it in your function
function SetPlayer () {
target = GameObject.FindGameObjectWithTag("Player");
active = false;
}
Answer by jenci1990 · Feb 11, 2015 at 03:15 PM
The problem is in player script: FindGameObject and FindGameObjects are not same.
GameObject.FindGameObjectsWithTag("Defenseman"); //This return array of GameObject.
you have to do this:
var listOfDefenseman : GameObject[] = GameObject.FindGameObjectsWithTag("Defenseman");
for (defman : GameObject in listOfDefenseman) {
defman.SendMessage("SetPlayer");
}
If you have one defenseman script use this:
GameObject.FindGameObjectWithTag("Defenseman").SendMessage("SetPlayer");