エラーの内容は
「UnassignedReferenceException: The variable TrapSpike1 of Traps has not been assigned.
You probably need to assign the TrapSpike1 variable of the Traps script in the inspector.
UnityEngine.GameObject.GetComponent[T] () (at <2fae0a4cbcec42c9acc616494aa88f69>:0)
Traps.Start () (at Assets/Scripts/Traps.cs:16)」
です。
TrapSpike1変数がTrapsのインスペクターにアサインされていないということだと思うのですが、インスペクター上ではドラッグ・アンド・ドロップはしています。
ここから更にどうすればよいのでしょうか。
Trapsのコードは以下です。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Traps : MonoBehaviour { public GameObject TrapSpike1; public float trap1jumpPower = 500; Rigidbody2D rigidTrap1; void Start() { rigidTrap1 = TrapSpike1.GetComponent<Rigidbody2D>(); } // Update is called once per frame public void Trap1Activate() { rigidTrap1.AddForce(Vector2.up * trap1jumpPower); Debug.Log(rigidTrap1);//NULLになります } } コード
Trapsを呼び出すコードは以下です。
using UnityEngine; public class PlayerManager : MonoBehaviour { //public public float xspeed; public float jumpPower; public GroundCheck ground; public GameManager gameManager; //SE [SerializeField] AudioClip jumpSE; //private Traps trap1; private bool isGround = false; float speed; bool isDead = false; AudioSource audioSource; public enum DIRECTION_TYPE { STOP, RIGHT, LEFT } DIRECTION_TYPE direction = DIRECTION_TYPE.STOP; Rigidbody2D rigidbody2D; Rigidbody2D trapSpike1; Animator animator; private void Start() { rigidbody2D = GetComponent<Rigidbody2D>(); trapSpike1 = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); audioSource = GetComponent<AudioSource>(); } void Update() { if (isDead) { return; } //接地判定を得る isGround = ground.IsGround(); float x = Input.GetAxis("Horizontal"); //speedというパラメータにxを代入 animator.SetFloat("speed", Mathf.Abs(x)); if (x == 0) { //止まっている direction = DIRECTION_TYPE.STOP; } else if (x > 0) { //右に動く direction = DIRECTION_TYPE.RIGHT; } else if (x < 0) { //左に動く direction = DIRECTION_TYPE.LEFT; } //スペース押したらジャンプ if (isGround) { if (Input.GetKeyDown("space")) { Jump(); } else { animator.SetBool("isJumping", false); } } switch (direction) { case DIRECTION_TYPE.STOP: speed = 0; break; case DIRECTION_TYPE.RIGHT: speed = xspeed; transform.localScale = new Vector3(1, 1, 1); break; case DIRECTION_TYPE.LEFT: speed = -xspeed; transform.localScale = new Vector3(-1, 1, 1); break; } rigidbody2D.velocity = new Vector2(speed, rigidbody2D.velocity.y); } void Jump() { //上に力を加える rigidbody2D.AddForce(Vector2.up * jumpPower); animator.SetBool("isJumping", true); audioSource.PlayOneShot(jumpSE); } private void OnTriggerEnter2D(Collider2D collision) { if (isDead) { return; } if (collision.gameObject.tag == "Trap") { Debug.Log("GameOver"); gameManager.GameOver(); PlayerDeath(); } if (collision.gameObject.tag == "Finish") { Debug.Log("GameClear"); gameManager.GameClear(); } //トラップSwitchのタグ if (collision.gameObject.tag == "Switch1") { Debug.Log("罠1起動"); Debug.Log(trap1); trap1.Trap1Activate(); } } void PlayerDeath() { BoxCollider2D boxCollider2D = GetComponent<BoxCollider2D>(); Destroy (boxCollider2D); rigidbody2D.velocity = new Vector2(0,0); rigidbody2D.AddForce(Vector2.up * 400); animator.Play("PlayerDeathAnimation"); gameManager.GameOver(); isDead = true; } } コード
回答1件
あなたの回答
tips
プレビュー