- Home /
The question is answered, right answer was accepted
Add a variable / parameter to a built in component
I want to add 2 integers to the built in boxcollider2d so that i know when hit, what coordinate it coresponds in my 2 dimensional array of a custom class, so is there any way to add those 2 parameters so i can change them like BoxCollider2D.x and BoxCollider2D.y (they are all on 1 object so i cant use actual position)
You can always inherit from it and make a new one with more data.
public class HitCollider : BoxCollider2D
{
float hitX;
float hitY;
}
Assets/HitCollider.cs(5,14): error CS0509: HitCollider': cannot derive from sealed type
UnityEngine.BoxCollider2D'
$$anonymous$$y bad, was an assumption that you could derive from it. Gotta love Unitys sealed classes. Honestly at this point, you ether make a master script that is apart of the same gameobject, or you create a boxcollider array. For the first method, a simple
void OnCollisionEnter(Collision collision)
{
ColliderPos pos;
if (pos = collision.GetComponent<ColliderPos>())
{
int x = pos.x;
int y = pos.y;
}
}
Follow this Question
Related Questions
Unity cannot find component of an object in an array. 2 Answers
Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers
access the animation array in an animation component. 1 Answer
script communicate with each other 0 Answers
problem with FindWithTag 2 Answers