This is for a snake game, and i have looked everywhere, and spent days doing it so this really is my final resort.
I am using two prefabs, one for a body to add to the snake, and another one for food. I have it set up so that at the start, the food randomly respawns, and after collision does create the instance of the body prefab. However, i need it to respawn again after collision, and it simply doesn’t. Only the once. How do i get to respawn again and again?
//Bodyprefab
var BodyPrefab : Transform;
// food prefab
public var pickupPrefab:GameObject;
// the spawnpoint to be spawned out
private var spawnPoint:GameObject;
//awake functions are called before the script starts
function Awake()
{
// retrieve GameObject tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
spawnPoint = gameObject.FindWithTag("SpawnPoint");
// spawn the pickup
SpawnPickup();
}
function OnControllerColliderHit (collision : ControllerColliderHit)
{
if(collision.gameObject.tag == "Player")
{
print ("FOOD!");
Instantiate(BodyPrefab, GameObject.Find("Snake").transform.position, Quaternion.identity);
//originally it did say "Destroy(gameObject)" but i assumed that was destroying the food prefab, with or without it didn't make a difference.
SpawnPickup();
}
}
function SpawnPickup()
{
// retrieve the position and rotation of the pickup's spawn point
var spawnedPickupPosition = spawnPoint.transform.position; // copy position
spawnedPickupPosition.z = Random.Range (-11.7,11.8);
spawnedPickupPosition.x = Random.Range(11.5,-11.9);
var spawnedPickupRotation:Quaternion = spawnPoint.transform.rotation;
// instantiate (create) the pickup prefab with the above position and rotation
var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);
// set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of
spawnedPickup.transform.parent = spawnPoint.transform;
}