I have 2 objects in my game, a sphere and a cube. The sphere starts at position A and the cube moves towards it using the following script -
#pragma strict
public var victim : Transform;
private var navComponent : NavMeshAgent;
function Start()
{
navComponent = this.transform.GetComponent(NavMeshAgent);
}
function Update()
{
if(victim)
{
navComponent.SetDestination(victim.position);
}
}
The sphere has a script on it that allows it to be destroyed and instantiated at a new location -
#pragma strict
public var resourceSpawnPoints : Transform[]; //Makes a list of transforms in my game
public var randomPos : int; //This denotes that the variable randomNum will be a whole number
public var resourceType : GameObject;
function OnTriggerEnter(thing: Collider)
{
if (thing.tag == "Harvester")
{
ResourceRespawn();
}
}
function ResourceRespawn()
{
randomPos = Random.Range(0,4); // generates random number between 0 and 3.
transform.position = resourceSpawnPoints[randomPos].transform.position;
//Assigns the number generated in the previous line to the 'spawnPoints' variable
//Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
Instantiate(resourceType, resourceSpawnPoints[randomPos].position, resourceSpawnPoints[randomPos].rotation);
Destroy (gameObject);
}
My problem is that once the cube/harvester reaches the sphere it activates the sphere's destroy/instantiate script but then stops. It doesn't then move towards the newly instantiated sphere.
Any suggestions as to why this happens (or doesn't happen to be more precise)
???
↧