- Home /
Question by
GameDBharat · Nov 28, 2019 at 06:25 AM ·
gameobjectrigidbody2dcollider2daddcomponent
When i try to add Collider2D or Rigidbody2D through code to prefab gameObject after stopping game the added component remain added to the prefab obejct.
How i want it when i run game collider or rigidbody component should get added to prefab object when it stop it should added component should be not there.
public class Tower : MonoBehaviour
{
TowerHouse _towerInstance;
public void Start()
{
AddTower();
}
public void AddTower()
{
TowerHouse _towerHouse;
GameObject builder_UpDownwall =(GameObject)Resources.Load("Prefabs/Structure/BuilderUpDownWall", typeof(GameObject));
if (builder_UpDownwall .GetComponents<Rigidbody2D>().Length >= 1)
{
DestroyImmediate(builder_UpDownwall .GetComponent<Rigidbody2D>(), true);
builder_UpDownwall .AddComponent<Rigidbody2D>().isKinematic = true;
}
else
{
builder_UpDownwall .AddComponent<Rigidbody2D>().isKinematic = true;
}
_towerHouse = builder_UpDownwall.GetComponent<TowerHouse>();
_towerHouse.GetComponent<SpriteRenderer>().sortingOrder = 1;
_towerInstance = Instantiate(_towerHouse, new Vector3(transform.position.x,
transform.position.y, transform.position.z), _towerHouse.transform.rotation);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612215408im_/https://answers.unity.com/themes/thub/images/avi.jpg)