- Home /
The question is answered, right answer was accepted
How do you get the component/access the script of the object you hit?
How do you get the component/access the script of the object you hit?
For example, when Object A hits Object B, I want Object A to have access to the scripts of Object B.
Also, Object A and Object B are CLONES. So I can't just use static because it would affect the other clones. I only want to modify the other object it hits, not the other objects. Also, I can't just get the component as is, because it would get the component of a random Object B, not the Object B it hits with its collider.
Here's an example code:
private ObjectB compo;
compo = GetComponent<ObjectB>();
void OnTriggerEnter(Collider col)
{
compo.Killed = false;
//There are many Object Bs, so this would probably get executed on a random Object B,
//instead of the Object B it hits.
}
Thanks!
Just for future reference, "using static" is never the answer...
Using static may be the answer in some cases, not this one. If static was that bad, the developer of .NET would not have included it and Unity would not use static for half of its implementation.
Answer by fafase · May 24, 2014 at 12:36 PM
void OnTriggerEnter(Collider col)
{
col.gameObject.GetComponent<Component>();
}
Follow this Question
Related Questions
Why there is no Collider.IsTouching(...) ? 2 Answers
Tilemap Collider 2D preventing objects from moving 2 Answers
Does disabling colliders increase performance? 2 Answers
OnTriggerEnter does not work 1 Answer