前提・実現したいこと
ロボットにポーズを取らせたい
ここに質問の内容を詳しく書いてください。
unityのゲーム開発でロボットにポーズを取らせたい。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
consoleで実行しマウスをクリックするとキャラクターロボットが消えてしまう。
エラーメッセージ
NUllReferenceException: Object reference not set to an instance of an object
Robotscript.start() (at.Aseets/Robotscript.cs:20)
該当のソースコード
using System.Collections.Generic;
using UnityEngine;
public class Robotscript : MonoBehaviour {
Dictionary<string, Sprite> pose = new Dictionary<string, Sprite>() { {"Nomal", null }, {"Right", null }, {"Left", null }, {"Both", null }, }; // Use this for intialization void Start(){ foreach(var key in new List<string>(pose.Keys)) { Texture2D tex = (Texture2D)Resources.Load("Robot_"+key); pose[key] = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f)); } } void Pose(string p) { GetComponent<SpriteRenderer>().sprite = pose[p]; } // Update is called once per frame void Update() { /* GetComponent<Transform>(). Translate(0.0f, 0.01f, 0.0f); Debug.Log(GetComponent<Transform>().position); */ if(Input.GetMouseButton(0) && Input.GetMouseButton(1)) { Pose("Both"); } else if(Input.GetMouseButton(0)) { Pose("Left"); } else if(Input.GetMouseButton(1)) { Pose("Right"); } else { Pose("Nomal"); } }
}
C#,unityここに言語名を入力
ソースコード
### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。