- Home /
How to respawn an object again after it has been instantiated
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;
}
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?
As in Epic by Faith No $$anonymous$$ore .... What is IT ?
It is hard to understand. So, snake hits Food, food gets destroyed, snake gets a body part, food is instantiated somewhere else randomly. This part I get.
Are you saying that more food is not instantiated, or when you collide with food no extra body part is instantiated?
No more food is instantiated, when i collide with food body part is instantiated. But because no more food spawns, neither can any more body parts.
Start off with some basic debugging to see what is going on in the collider function, eg :
function OnControllerColliderHit (collision : ControllerColliderHit)
{
Debug.Log( "Controller Hit : " + collision.gameObject.tag )
if(collision.gameObject.tag == "Player")
// etc
this should display in the console the tag of each collision, from here you should be able to see if the tag is Player, or something else that is stopping this from working.
Answer by 3Dx2Yz · Jan 22, 2013 at 12:11 PM
spawnPoint = gameObject.FindWithTag("SpawnPoint");
It seems you have a simple error in your code.
If you write "gameObject" you refer to your current GameObject this script it assigned on. If you want to find another Game Object from your Project, you need to write:
spawnPoint = GameObject.FindWithTag("SpawnPoint");
$$anonymous$$aybe try to change this line: var BodyPrefab : Transform; into this line: var BodyPrefab : GameObject;
Your answer
