- Home /
What to set to make a Instantiated prefab a collider to trigger and event?
Answer by liamcary · Feb 26, 2013 at 06:01 AM
Does the prefab already have a collider? If so you just need to mark the collider as a trigger. Then in a script on that same object you can use "OnTriggerEnter()", "OnTriggerStay()" and "OnTriggerExit()" for any functionality when something enters the trigger. If you want to add the collider to a particular instance of a prefab at run-time you can use the AddComponent function and set the collider as a trigger. E.g:
class Example : MonoBehaviour
{
public GameObject prefab;
void Awake()
{
SphereCollider sphere = prefab.AddComponent<SphereCollider>();
sphere.isTrigger = true;
}
void OnTriggerEnter(Collider other)
{
// do something
}
}
AddComponent function: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html
Your answer
Follow this Question
Related Questions
Player clone not instantiating at the same spot 1 Answer
Any ideas on a simple AI script that doesn't use any drag/drop variables 1 Answer
Multiplayer prefab player fall through floor on instantiate 0 Answers
Instantiating random prefabs when the player passes through a trigger 1 Answer
Problem when assigning a prefab 1 Answer