前提・実現したいこと
unityにてC#を用いて、特定の状態のときに作用するシステムを作りたい。
具体的には、”count”が1以上のとき”Player”タグオブジェクトが”Door”に接触すると”Door”が1秒後に削除される仕組み。
試したことにもありますが、if構文の問題ではなくUnity側の問題でしょうか。
回答お願い致します。
発生している問題・エラーメッセージ
言語 C#
エラーメッセージ なし ~~public class Door : MonoBehaviour { public int count; void OnCollisionEnter(Collision other) { if (other.gameObject.CompareTag("Player")) { if (count >= 1) { Destroy(gameObject, 1.0f); } } } }`~~ ```編集後スクリプト
public class Door : MonoBehaviour
{
private void Start()
{
int Dcount;
Dcount = PlayerController_Complete.count;
}
void OnCollisionEnter(Collision other) { if (other.gameObject.CompareTag("Player")) { if (Dcount >= 1) { Destroy(gameObject, 1.0f); } } }
}
![イメージ説明](413bf10f79629f386f4a9d025a33c44f.png) Dcountのコードを追加したところ、このようなエラーが吐き出されました。 DoorのスクリプトにPlayerのcountスクリプトを入れようと思った次第です。 ### 試したこと 1)Doorオブジェクトのスクリプト内でcountを5にするとDoorは削除された。 2)Playerオブジェクトのスクリプト内でcountを5にしてもDoorは削除されなかった。 1),2)から、if構文自体に問題はないが、Player側のcountとDoor側のcountが同一のものとして扱われていない? ### 補足情報(FW/ツールのバージョンなど) unity2018.3.0f2 Visual Studio COmmunity 2017 ver 15.9.5 Player側のスクリプトを記載しておきます。 回答よろしくお願いします。
public class PlayerController_Complete : MonoBehaviour
{
Rigidbody rb; public float speed; public int count; public Text countText; // Use this for initialization void Start() { rb = GetComponent<Rigidbody>(); count = 10; SetCountText(); } // Update is called once per frame void FixedUpdate() { float moveH = Input.GetAxis("Horizontal"); float moveV = Input.GetAxis("Vertical"); Vector3 move = new Vector3(moveH, 0, moveV); rb.AddForce(move * speed); } public void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("A")) { other.gameObject.SetActive(false); count = count + 1; SetCountText(); } else if (other.gameObject.CompareTag("B")) { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } } void SetCountText() { countText.text = "ゲット数:" + count.ToString(); }
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。