Tower defense: pigs, towers and SendMessage xD

Hello people, I would appreciate a little of your time and your help.

I have 3 prefabs (pig (tagged as Enemy), tower, spawner), with this 3 scripts:

Spawner:

var animal : Transform;
var time = 1.0;

function Start () {
	for(var i = 0; i < 20; i++) {
		Instantiate(animal, transform.position, transform.rotation);
		yield WaitForSeconds (time);
	}
}

this creates 20 pig(Clone)

Pig:

function Update () {
}

function Die(){
	Destroy(gameObject);
}

Tower:

var target: GameObject; 
var reach = 1000.0;

function Start () {
	if (target.transform == null  GameObject.FindWithTag("Enemy")){
		target = GameObject.FindWithTag("Enemy");
	}
	
}

function Update () {
	if (target.transform == null){
		return;
	}
	
	if (Vector3.Distance(transform.position, target.transform.position) <= reach){
		transform.Translate(0,1,0);
		target.SendMessage ("Die");
	}
}

My dear tower moves up like a rocket (so it finds enemies at range), but my dear pig(Clones) don’t want to die. What am I doing wrong?. Thanks for your time and your help :smile:.

hmm well you won’t get much help from me (I’m beginner and I use c#)

but
1.) TD should have projectiles (for towers) as I know but if you have nice idea without projectiles it can be nice
2.) I don’t see connection between Pig’s “function Die()” and Tower’s code

3.) towers goes up because of

“transform.Translate(0,1,0);” (Translate(x,y,z) and you know that axis-y goes up/is vertical (by default))

so that means your update function (it is called every frame) reads like this:
-I’m checking distance between tower and pig (I think) and if the pig is inside of the range then do:
-move tower by 1 ON Y AXIS (that means up)
-and send message “Die”

I don’t see and relations with Pig’s function Die.

solution could be if you replace function Die into the tower’s code like this:

if (Vector3.Distance(transform.position, target.transform.position) <= reach){
transform.Translate(0,1,0);
Destroy(target);
target.SendMessage ("Die");

but ofcourse with this code, pig would instantly die when it comes in range of the tower, you can make health to the pig like hp=10
and then you can make a cooldown to tower’s attack like cd=120
and for each update decrease cd by 1
and for each hit decrease hp by 1
then your tower’s update function would look like this:

if (cd > 0)
{
cd = cd - 1;   //cd-- is same thing
}

if (Vector3.Distance(transform.position, target.transform.position) <= reach  cd == 0)
{
cd = 120;
hp = hp - 1;
}

if (hp == 0)
{
Destroy(target);
}

WARNING: I’ve made this code for game when you have 1pig only and 1 tower only, if you have more towers then you should array cd variable and each index attach to one tower, also if you have more pigs (and you have) you should array hp variable and attach each index to one pig

if (this was helpful == true)
I’m happy;
else
I hope someone else will help you;

Thanks for the help, but in this case I just want to know why SendMessage is not working. In theory SendMessage calls the method named Die on every MonoBehaviour in pig, but it does nothing.

The fact of tower moving up is a way for me to know if there are pigs in range. Of course in a future I will use health like you said. My idea is to create a method called Damage(var amount) at the pig script (it will lower pig health in an amount, and kill him if his final health is below 1). My idea is is to call this method from tower, using tower damage as amount, using SendMessage.

FindWithTag() only returns 1 game object, so even if nothing else is broken, only 1 pig will be destroyed.

You probably want to use FindGameObjectsWithTag() and send a message to each one:

http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html