- Home /
Question by
gaia2222 · Jun 17, 2020 at 07:25 AM ·
collider2dclassassign-variable
Can not assign collider2D to the other class
public class enemydata{
public GameObject go;
public Transform t;
public Collider2D col;
public enemydata(GameObject go,Transform t, Collider2D col)
{
this.go = go;
this.t = t;
this.col = col;
}
}
And in the other script:
col = GetComponent<Collider2D>();
testdata.addtolist(new enemydata(gameObject, gameObject.transform, col));
The col can not assign the collider2D to col, Can someone suggest the other method for me please?
Comment
Answer by ilusja · Jun 17, 2020 at 07:53 AM
I guess you meant something like:
public enemydata(GameObject go,Transform t)
{
this.go = go;
this.t = t;
col = go.AddComponent<Collider2D>();
}
Or:
public class EnemyData : MonoBehaviour
{
public GameObject go;
public Transform t;
public Collider2D col;
public EnemyData(GameObject go,Transform t, Collider2D col)
{
this.go = go;
this.t = t;
go.AddComponent<Collider2D>();
this.col = go.GetComponent<Collider2D>();
}
}
But then, you don't actually pass first Collider2D to the game object. I'm not sure if it's even possible to do this way.
Also, avoid using only small letters in your classes names. I would propose naming it: EnemyData
. Also, are you sure you don't want your EnemyData
to inherit from the MonoBehaviour
class?
Your answer
![](https://koobas.hobune.stream/wayback/20220613001136im_/https://answers.unity.com/themes/thub/images/avi.jpg)