Unity 2D Spawn on trigger
Hi, I'm making a 2D game and I want my object to respawn after it have collided with my player. What is missing in this code?
using UnityEngine; using System.Collections;
public class FOOD_SPAWN : MonoBehaviour {
public GameObject FOODPREFAB;
// Borders
public Transform TOP;
public Transform BOT;
public Transform LEFT;
public Transform RIGHT;
void onTriggerEnter(Collider co){
if (co.name == "FOODPREFAB") {
Spawn();
}
}
void Spawn() {
// x position between left & right border
int x = (int)Random.Range(LEFT.position.x, RIGHT.position.x);
// y position between top & bottom border
int y = (int)Random.Range(BOT.position.y, TOP.position.y);
// Instantiate the food at (x, y)
Instantiate(FOODPREFAB, new Vector2(x, y), Quaternion.identity); // default rotation
}
}
Comment
Your answer
Follow this Question
Related Questions
2D player enter/exit orbit 0 Answers
Help! Create interactive button with an image + c# Script 0 Answers
Player Respawn Problem, Bullets dont move! [C#] 0 Answers
Unity 2D - Material for light 2 Answers
Simpsons Tapped Out Type Camera? 0 Answers