質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

5947閲覧

オブジェクトをインスペクターにアタッチしましたがアサインされません

退会済みユーザー

退会済みユーザー

総合スコア0

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2021/04/23 06:33

編集2021/04/23 07:24

エラーの内容は
「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; } } コード

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2021/04/23 07:26

画像で見せたゲームオブジェクト以外のゲームオブジェクトにTrapsがアタッチされていませんか?
退会済みユーザー

退会済みユーザー

2021/04/23 07:42

アタッチしてしまってました。 ありがとうございます。助かりました。
guest

回答1

0

ベストアンサー

別のオブジェクトにTrapsをアタッチしてしまっていました。
修正したら直りました。

投稿2021/04/23 07:43

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問