Assigning a spawner object as a target transform for an AI, I'm clueless :I

Hi!,

I’m having some problems with a monster prefab I’m trying to make, I basically have a monster object with its AI script and a spawner object.

In the AI script I need to manually assign 2 transforms to it in order for it to work, one for its spawn point and another one for what it’s supposed to attack.

I’ve made it so that when I put a spawner into my scene it automatically spawns one monster, however after that I’ll still need to assign those 2 transforms manually.

I’ve been struggling to get either my spawner script or my monster AI to automatically pick those transforms from the hierarchy, and assign them to the variables in my monsters AI. So far I’ve had no luck in getting this to work properly, but from what I can figure out I need to add a GameObject.Find or GameObject.FindWithTagg to my AI script.
However when I just try using the bits of code used in the help docs it gives me a whole bunch of errors. So before completely messing up what I had so far I’d decided to ask how to do this properly. Any help on how to assign those two objects to my variables automatically would be great!:smiley:

EDIT////////////////////

I tried a couple things out and it seems I found my solution :smiley:

adding this code to the awake function worked out fine:

SpawnPoint = GameObject.Find("SpawnGizmo");
spawn = SpawnPoint.transform;
Player = GameObject.Find("Player");
victim = Player.transform;

Another EDIT: It seems now tho that I need to make sure that the SpawnPoint is the SpawnGizmo that actually created the monster. Since when I add more monsters they all assign to the first gizmo. Any tips would be appreciated :smiley:

for the player(or whoever needs to be attacked), use this:

GameObject.FindGameObjectWithTag("Player");

for the spawner, when you instantiate it use something like:

var monster = Instantiate(monster, transform.position, Quaternion.identity);
monster.parent = transform;
monster.GetComponent<AIScript>().spawnervariable = monster.parent;

You should edit your question and post the AI script! Anyway, the search for a target object is a very common task. It’s done at Start with FindWithTag. The spawn point, however, isn’t clear. If you already spawned the monster somewhere, why should it need to find a spawn point?

Basically, what you have to do is something like this in the AI script:

var target: Transform;
var spawnPoint: Transform;

function Start(){
    var go1 = GameObject.FindWithTag("YourTarget"); // look for the target
    if (go1){
        target = go1.transform; // if target object found, set target...
    } else {
        Debug.Log("Player not found!"); // if not, warn us!
    }
    var go2 = GameObject.FindWithTag("YourSpawnTag");
    if (go2){
        spawnPoint = go2.transform; // if spawn object found, set spawnPoint
    } else {
        Debug.Log("Spawn object not found"); // if not, tell us!
    }
}

There’s a lot of variations around this, since you’ve not posted your script. The tags “YourTarget” and “YourSpawnTag” are obviously symbolic - you should give to both objects meaningful tags, and use them in FindWithTag. Maybe the spawnPoint should be used by the monster creator script - just place those instructions in its Start function, if that’s the case.

Another point: if there are several spawn objects in your scene, this routine will always return the same one (the first of them it found).

Haha, sorry, didn’t expect answers so fast so I tried some things myself. Now the only real problem is that it always selects the first spawner.

Also, in the AI the spawnpoint is actually used as a pointer for the monster to return to when the player is out of reach or when it dies. I suppose I could post the AI script and the small spawner script I made, maybe just to see if there’s a better way to go around creating a spawnpoint. So, here’s the AI script:

private var SpawnPoint: GameObject;
var spawn:Transform;
var victim : Transform;
var speed : float;
var rotationSpeed : float;

var damage : int;
var dice : int = 1;
var basedamage : int = 1;

private var myTransform : Transform;

private var idle : int = 0;
private var returnToSpawn : int = 1;
private var chasePlayer : int = 2;
private var attackPlayer : int = 3;
private var dead : int = 4;

private var status = returnToSpawn;
private var monsterStats : String;
private var seconds : int;
private var savedTime : int;

function Awake()
{
	myTransform = transform;
	SpawnPoint = GameObject.Find("SpawnGizmo");
	spawn = SpawnPoint.transform;
	Player = GameObject.Find("Player");
	victim = Player.transform;

}

function Update () 
{

	var target : Transform;
	var dist = Vector3.Distance(spawn.position,myTransform.position);
	var aggro = Vector3.Distance(victim.position,myTransform.position);
	var statusString:String;
	seconds = Time.time;
	
	if (dist > 50 || (aggro > 20 && dist > 3))
		{
			status = returnToSpawn;
			animation.CrossFade("walk");
		}
	else if (dist < 30 &&(aggro < 20 && aggro > 1))
		{
			status = chasePlayer;
			animation.CrossFade("walk");
		}
	else if (aggro < 10)
		{
			status = attackPlayer;
			animation.CrossFade("walk");
		}
		
	switch(status)
	{
		case returnToSpawn:
			target = spawn;
			myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position-myTransform.position),rotationSpeed * Time.deltaTime);
			myTransform.position += myTransform.forward * (speed/2) * Time.deltaTime;
			statusString = "Return";
			if (dist < 3)
				status = idle;
			break;
			
		case chasePlayer:
			target = victim;
			myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position-myTransform.position),rotationSpeed * Time.deltaTime);
			myTransform.position += myTransform.forward * speed * Time.deltaTime;
			statusString = "Chase";
			break;
		
		case attackPlayer:
			target = victim;
			myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position-myTransform.position),rotationSpeed * Time.deltaTime);
			statusString = "Attack";
			
			if (aggro > 1)
			{
				status = chasePlayer;
			}
				
			if (seconds != savedTime)
			{
				damage = ((Random.Range(1,basedamage))*(Random.Range(1,dice)));
				savedTime = seconds;
			}
			break;
			
		case idle:
			target = spawn;
			statusString = "idle";
			animation.CrossFade("idle");
			break;
			
		default:
			break;
	}
		myTransform.eulerAngles = new Vector3(0, myTransform.eulerAngles.y,0);
		monsterStats = "Spawn Distance: " + dist + "
Victim Distance: " + aggro + "
Status: " + statusString + "
Damage: " + damage + "
seconds: " + seconds + "
savedTime: " + savedTime;
	
}

And here’s the little spawner script attached to my SpawnGizmo:

var mob: Transform;
var once = false;
function Update () 
{
	if (!once)
	{
		var mob = Instantiate(mob,transform.position, transform.rotation);
		once = true;
	}
}