問題点
実行した際に、すべてtrueに返ってしまっている。
また、falseにも返らない。
c#
1public class Player : MonoBehaviour 2{ 3 4 public bool up, down, right, left;//移動許可用変数 5 6}
プレイヤーにこのようなフィールドを用意しています。
これはコメント通り、これらの変数がtrue
を返している場合のみ動ける。というものになっています。
Hierarchy内のPlayerの構成
それぞれがScene内にあるTile
というオブジェクトに接している時のみ、true
にする。という構成を考えています。
Up
というオブジェクトの場合の構成です。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Up_tileCol : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Tile")
{
Player.playerInstance.up = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Tile")
{
Player.playerInstance.up = false;
}
}
}
子要素になっているオブジェクトのスクリプトはこのように組んでいます。
public class Tile : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
GameManager.gameManager.tileCount++;
gameObject.SetActive(false);
}
//if (collision.gameObject.tag == "Up")
//{
// collision.gameObject.GetComponentInParent<Player>().up = true;
//}
//if (collision.gameObject.tag == "Down")
//{
// collision.gameObject.GetComponentInParent<Player>().down = true;
//}
//if (collision.gameObject.tag == "Left")
//{
// collision.gameObject.GetComponentInParent<Player>().left = true;
//}
//if (collision.gameObject.tag == "Right")
//{
// collision.gameObject.GetComponentInParent<Player>().right = true;
//}
}
}
また、```tile```は以上のようなスクリプトになっています。 どのタイミングで、trueに返っているのか。なぜ、falseに返らないのかがわかりません。 理由がわかる方がいましたら、ご教授していただけるとうれしいです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。