- Home /
Ai Prefab Spawning in Without a target?
Hi everyone, i have a code that basically makes the AI wonder around, my problem is that when my AI spawning system spawns them in, all there targets disappear, it wouldn't be a issue if i just have to find the player (I have already done this) but i have like 100 way points.
#pragma strict
var spawnPoints : Transform[];
var yieldTimeMin = 2;
var yieldTimeMax = 5;
function Start()
{
// create a temporary to store gameObjects by finding with tag
var spts : GameObject[] = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
// set the size of the spawnpoints array
spawnPoints = new Transform[ spts.Length ];
// store a reference to each transform from the gameObject array
for ( var i : int = 0; i < spts.Length; i ++ )
{
spawnPoints[i] = spts[i].transform;
}
// start moving
Move();
}
function Move()
{
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
var target : Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
GetComponent(NavMeshAgent).destination = target.position;
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));
Move();
}
Ah ok, i may see your issue, in unity, you can't assign objects(or Transforms) to a prefab from the editor, so in this case, if you have added the point to "spawnPoints" in the editor then saved the prefab, the list will be empty.
Try having a central script with a list holding all of these points, then call these into your AI script after the the AI object is in the game!
Yes you can call Start.
The problem is when you instantiate the NPC, the array spawnPoints is not populated?
In that case you'll have to populate that array each time you instantiate a NPC. IF you have set a tag and assigned it to all spawn points, you can use GameObject.FindGameObjectsWithTag :
var spawnPoints : Transform[];
var yieldTime$$anonymous$$in = 2;
var yieldTime$$anonymous$$ax = 5;
function Start()
{
spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
$$anonymous$$ove();
}
function $$anonymous$$ove()
{
yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
var target : Transform = spawnPoints[Random.Range(0, spawnPoints.length)];
GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
yield WaitForSeconds(Random.Range(yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax));
$$anonymous$$ove();
}
Ok yeah, the best option for you would be to change the variable from a list of Trasnforms to a list of GameObjects! then just you "gameObject.transfrom.position"
Example using a timer :
#pragma strict
public var spawnPoints : GameObject[];
public var yieldTime$$anonymous$$in : float = 2;
public var yieldTime$$anonymous$$ax : float = 5;
private var timer : float = 2;
function Start()
{
// reset timer
timer = Random.Range( yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax );
// store gameObjects by finding with tag
spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
}
function Update()
{
// decrease timer
timer -= Time.deltaTime;
if ( timer < 0 )
{
// reset timer
timer = Random.Range( yieldTime$$anonymous$$in, yieldTime$$anonymous$$ax );
// find a target spawnpoint
var target : Transform = spawnPoints[ Random.Range(0, spawnPoints.Length) ].transform;
// set the navmesh waypoint
GetComponent(Nav$$anonymous$$eshAgent).destination = target.position;
Debug.Log( target.position );
}
}
Answer by AlucardJay · Jan 16, 2014 at 02:18 PM
See the comments for the full story!
The problem is when you instantiate the NPC, the array spawnPoints is not populated? If you have set a tag and assigned it to all spawn points, you can use GameObject.FindGameObjectsWithTag
When I started coding and asking questions, I really found it annoying when the answer given was "really you shouldn't be doing it like this", but .... really you shouldn't be doing it like this. You should use a timer, Invoke or a coroutine.
Here's an example using a timer :
#pragma strict
public var spawnPoints : GameObject[];
public var yieldTimeMin : float = 2;
public var yieldTimeMax : float = 5;
private var timer : float = 2;
function Start()
{
// reset timer
timer = Random.Range( yieldTimeMin, yieldTimeMax );
// store gameObjects by finding with tag
spawnPoints = GameObject.FindGameObjectsWithTag( "SpawnPoint" );
}
function Update()
{
// decrease timer
timer -= Time.deltaTime;
if ( timer < 0 )
{
// reset timer
timer = Random.Range( yieldTimeMin, yieldTimeMax );
// find a target spawnpoint
var target : Transform = spawnPoints[ Random.Range(0, spawnPoints.Length) ].transform;
// set the navmesh waypoint
GetComponent(NavMeshAgent).destination = target.position;
Debug.Log( target.position );
}
}
Thanks once again, also thanks to Callum, you guys have been great help