Trouble getting a script to work.

Hi! I’m not a very experienced scripter, so please bear with me. Here’s my script;

var newObject: GameObject; 

function Start() { 
   var go = GameObject.Find("Tank1");
   if (null) {
      yield WaitForSeconds(5); 
      Instantiate(newObject, transform.position, transform.rotation); 
   } 
}

I am attaching this script to an empty GameObject named Respawn1. What I want to happen is I want the script to detect when the GameObject Tank1 is destroyed, and then instantiate a new game object in 5 seconds at the location of Respawn1. My script isn’t doing anything, however… Help, please?

you need the if statement in an update loop so it keeps checking if its null (start is only called once).

i would probably just put a script on your tank that when it gets destroyed it sends a message to the spawner to make a new one in 5 secs. (the fps tut has examples of objects talking to each other via sendmessage broadcastmessage)

hope that helps ; )

EDIT: sorry maybe that’s confusing, try this (not tested):
(edit once more!)

//assumes you already have a tank in the scene at start
var newTank: GameObject; // your spawn object
var currentTank: GameObject;

var waiting : boolean = false; 

function Start ()
{ 
   // get tank in scene
   currentTank = GameObject.Find("Tank1");
}

function Update ()
{ 
   CheckTankStatus ();
}

function CheckTankStatus ()
{
   if (currentTank == null  !waiting)
      { 
          waiting = true;
          yield WaitForSeconds(5); 
          Instantiate (newTank, transform.position, transform.rotation);
          currentTank = newTank;
          waiting = false;
      } 
}

I was looking for instructions on that at one point, I should have checked the FPS tutorial! Thanks.

I get an error when trying to compile this script.

Script error: Update() can not be a coroutine.

{edit} Nevermind that, I didn’t grab the whole script :roll: I’ll try it again.

{edit2} It works, thanks! Now all I need to do is get my camera with the smoothfollow script attached to refocus on Tank1(Clone) :roll: Thanks for your help!

no you did i just edited it again because i’m a dope ; )

hopefully that does it - i usually shy away from posting scripting help as i’m not that good at it but this should work - someone else will probably come along with a better way though.

It did work, thanks. I just need to figure out how to handle this new bug and I’ll be set.

ok shlimazel, i’ve tested now this works. (sorry for the confusion i should never post when i don’t have the time to verify anything ; )

//assumes you already have a tank in the scene at start
var newTank: GameObject; // your spawn object
var currentTank: GameObject;

var waiting : boolean = false; 

function Start ()
{ 
   // get tank in scene
   currentTank = GameObject.Find ("Tank1");
}

function Update ()
{ 
   CheckTankStatus ();
}

function CheckTankStatus ()
{
   if (currentTank == null  !waiting)
      { 
          waiting = true;
          yield WaitForSeconds(5);
          var nTank : GameObject;
          nTank = Instantiate (newTank, transform.position, transform.rotation);
          currentTank = nTank;
          waiting = false;
      } 
}

Yeah that does work, thanks for helping me out!

Er, why does it call the new tank Tank1(Clone)? I have a camera which is using smoothfollow to follow Tank1, but since the new tank is not called Tank1 it doesn’t follow the new tank. Is there a way to make the script generate a tank without the (Clone) added on, or should I seek an alternative?

I think the idea of having the tank send a message when destroyed is the way to go. :slight_smile: No need to check every frame.

Put this on the script for the tank:

function OnDisable() {
	var respawnScript : Respawn = FindObjectOfType(Respawn);
	respawnScript.Respawn();
}

And put the following script on the empty GameObject (this assumes the script below is named Respawn; if not, change “Respawn” in the script above to what the script is actually called):

var newObject: GameObject; 

function Respawn() {
	yield WaitForSeconds(5);
	Instantiate(newObject, transform.position, transform.rotation); 
}

Edit: If you want to change the name, then change the instantiate code to:

var newTank = Instantiate(newObject, transform.position, transform.rotation);
newTank.name = "Tank1";

–Eric

Eric5h5, I used your script and it also worked. It generated a Tank1 instead of a Tank1(Clone) as well, which is handy, but I think I need to adjust the SmoothFollow script so that it checks for a new Tank1, or something like that, because it didn’t switch the camera focus to the newly spawned tank. Well, that’s probably a minor problem. Thanks for your help!

Yeah, actually I just thought of that.

var newObject: GameObject; 

function Respawn() { 
   yield WaitForSeconds(5); 
   var newTank = Instantiate(newObject, transform.position, transform.rotation);
   var followScript : SmoothFollow = FindObjectOfType(SmoothFollow);
   followScript.target = newTank.transform;
}

That should do it…I think… (That way it doesn’t matter what the tank is called, either.)

–Eric

It doesn’t seem to make the camera switch to follow the new tank. Is there something I should have done (besides update the Respawn script)?

I’ll mess with it and see if maybe I screwed up somewhere.

[edit]

Actually it looks like it is focusing the wrong camera, but it did work. Thanks! That’s one of two major hurdles.

[edit2]

Works great! No remaining bugs! Thanks all!