- Home /
onEnterCollider returns Collider
Hello
I have been trying to create function of
onEnterCollider (Collider other){}
so it would return Collider of GameObject that entered it, so I would be able to access it from other class
So I wrote something like :
public Collider OnTriggerEnter(Collider other)
{
if (other.tag == "Player") return other.GetComponent<Collider>();
else return null;
}
and now I am trying to access it from another class
if (LightRepair.instance.OnTriggerEnter(other) != null)
{
}
but it keeps showing me error of other is not defined in this context, however I also tried doing sth like this :
if (LightRepair.instance.OnTriggerEnter(LightRepair.instance.other) != null)
{
}
But it is still the same error, how can I access it? Maybe my way of thinking is wrong xd dunno Hope you will be able to answer it
Thank you for your help
Answer by Hellium · Dec 09, 2017 at 10:06 PM
Your code does not make sense.
OnTriggerEnter
is a message sent automatically by Unity. If the function signature is not void OnTriggerEnter(Collider other)
, it won't be called.
What you have to do is the following :
Keep the original OnTriggerEnter
and store the collider as a class member. Then, you will be able to retrieve it from the outside of the class.
// LightRepair class
public Collider PlayerCollider
{
get ; private set ;
}
private Collider OnTriggerEnter(Collider other)
{
if (other.tag == "Player") PlayerCollider = other ;
else PlayerCollider = null ;
}
// Other class
if (LightRepair.instance.PlayerCollider != null)
{
}
O$$anonymous$$G so this is what get and set are for! Thank you so much :)
Your answer
Follow this Question
Related Questions
Strange OnTriggerEnter/Exit() Behavior 0 Answers
How to detect collisions in C# 2 Answers
OnTriggerEnter() not working 1 Answer
How to have a score counter increment in OnTriggerEnter 1 Answer