コード例 #1
0
 // 2Dの場合のトリガー判定
 private void OnTriggerEnter2D(Collider2D collision)
 {
     // 物体がトリガーに接触しとき、1度だけ呼ばれる
     if (collision.gameObject.name == "Player")
     {
         foreach (GameObject On in OnObj)
         {
             On.GetComponent <SpriteRenderer>().enabled = true;
             On.GetComponent <Collider2D>().enabled     = true;
         }
         foreach (GameObject Off in OffObj)
         {
             Off.GetComponent <SpriteRenderer>().enabled = false;
             Off.GetComponent <Collider2D>().enabled     = false;
         }
         Destroy(this.gameObject);
     }
 }
コード例 #2
0
    // Use this for initialization

    void OnCollisionEnter2D(Collision2D col)
    {
        playerObj = GameObject.FindWithTag("Player");
        UnityChan2DController unityChan2DController = playerObj.GetComponent <UnityChan2DController>();

        if (col.gameObject != playerObj)
        {
            foreach (GameObject On in OnObj)
            {
                On.GetComponent <SpriteRenderer>().enabled = On.GetComponent <SpriteRenderer>().enabled ^ true;
            }
            foreach (GameObject Off in OffObj)
            {
                Off.GetComponent <SpriteRenderer>().enabled = Off.GetComponent <SpriteRenderer>().enabled ^ true;
            }
            foreach (GameObject Des in DestroyObj) //スイッチにより発生するオブジェクトの処理
            {
                Des.GetComponent <SpriteRenderer>().enabled  = Des.GetComponent <SpriteRenderer>().enabled ^ true;
                Des.GetComponent <BoxCollider2D>().isTrigger = Des.GetComponent <BoxCollider2D>().isTrigger ^ true;
            }
        }
    }