- Home /
My Spawn Point doesn't ignore random game objects. How do I fix?
I'm making a 2.5D platformer and I have a script set up that triggers an enemy spawn when the player enters the cube (the object I used as the trigger. It's set to "Is Trigger."). It spawns the "enemy" cube where I want and everything, but unfortunately the trigger doesn't differentiate between my player or random game objects that I have sent flying with my physics. How can I get the trigger to ignore random objects and only go off when the player has walked through? (I'm using C#).
using UnityEngine;
using System.Collections;
public class SpawnPoint : MonoBehaviour {
public Collider player;
public Collider spawnPoint1;
public Rigidbody enemy;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Functions
void OnTriggerEnter(Collider player){
Rigidbody instance = Instantiate(enemy, spawnPoint1.transform.position, transform.rotation) as Rigidbody;
}
}
Then when I get into Unity I set my character as "player," the place where the enemies spawn as "spawnPoint1" and my enemy prefab as "enemy."
By the way my code might look strange because I'm still trying to understand how the mechanics on ontriggerenter and instantiate work... so I was using some tutorials as a reference as I wrote it.
Answer by iwaldrop · Feb 10, 2013 at 06:19 AM
Make sure the Tag for your Player GameObject is set to "Player" in the drop-down Tag selector, and then:
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
Rigidbody instance = Instantiate(enemy, spawnPoint1.transform.position, transform.rotation) as Rigidbody;
}
Your answer
Follow this Question
Related Questions
onTrigger spawn object C# 3 Answers
Spawn Point Problems 1 Answer
Code based 3d multiple cube instantiation 1 Answer
Instantiate over time? 1 Answer
Networking: Object is instantiated twice instead of once 1 Answer