https://webhoric.com/apple/mac/unity-2d-sprite-change/
このサイトを参考に画像をタップして別の画像に切り替えるように実装したのですが
NullReferenceException: Object reference not set to an instance of an object chgSprite.changeSprite () (at Assets/Scripts/chgSprite.cs:15)
というエラーがでます。
実際のコード↓
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class chgSprite : MonoBehaviour { 7 8 public Sprite spriteMae; 9 public Sprite spriteAto; 10 private bool chFlg = false; 11 public void changeSprite() 12 { 13 if (!chFlg) 14 { 15 this.gameObject.GetComponent<Image>().sprite = spriteAto; 16 chFlg = true; 17 } 18 else 19 { 20 this.gameObject.GetComponent<Image>().sprite = spriteMae; 21 chFlg = false; 22 } 23 } 24
インスペクターのspriteMae,spriteAtoに画像を指定しましたができませんでした。
unityのバージョンは2018.2.17f1 personalです。
この書き方以外にも方法があれば教えていただけたら嬉しいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/28 12:41
2019/01/28 13:21
2019/01/28 14:10
2019/01/28 14:14