前提・実現したいこと
canvasで画像をクリア時に出るようにしたのですが、下記のようなエラーが出ていて
出しっぱなしにしておくのも気持ち悪いです。(動作に問題はない)
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object canvas.displayTelop (System.String telopName) (at Assets/player/canvas.cs:54) UnityEngine.GameObject:SendMessage(String, Object) <clear>d__17:MoveNext() (at Assets/player/player.cs:242) UnityEngine.MonoBehaviour:StartCoroutine(String) player:OnTriggerEnter2D(Collider2D) (at Assets/player/player.cs:264)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using DG.Tweening; 6 7public class canvas : MonoBehaviour 8{ 9 10 private RectTransform rectTransform; 11 12 private GameObject imageObject; 13 14 private Image imageComponent; 15 16 private Text Scoretext; 17 18 private RectTransform fadeRectT; 19 20 private GameObject fadeObject; 21 22 public GameObject textObject; 23 24 public int Score = 0; 25 26 27 // Start is called before the first frame update 28 void Start() 29 { 30 imageObject = GameObject.Find("Image"); 31 imageComponent = imageObject.GetComponent<Image>(); 32 imageComponent.enabled = false; 33 34 35 36 textObject = GameObject.Find("Text"); 37 Scoretext = textObject.GetComponent<Text>(); 38 Scoretext.text = "000000"; 39 40 fadeObject = GameObject.Find("fade"); 41 fadeRectT = fadeObject.GetComponent<RectTransform>(); 42 43 44 45 fadeIn(); 46 } 47 48 void displayTelop( string telopName ) 49 { 50 imageComponent.enabled = true; 51 52 Texture2D telopImage = Resources.Load(telopName) as Texture2D; 53 54 imageComponent.sprite = Sprite.Create(telopImage, 55 new Rect(0, 0, telopImage.width, telopImage.height), 56 Vector2.zero); 57 58 imageComponent.SetNativeSize(); 59 } 60 61 void addScore(int score) 62 { 63 Score += score; 64 65 Scoretext.text = Score.ToString("000000"); 66 } 67 68 void fadeIn() 69 { 70 fadeRectT.DOScale(new Vector3(1, 0, 1), 1.5f) 71 .SetEase(Ease.InOutQuint); 72 } 73 74 void fadeOut() 75 { 76 fadeRectT.DOScale(new Vector3(1, 1, 1), 1.5f) 77 .SetEase(Ease.InOutQuint); 78 } 79} 80
補足情報 Unity 2020.2.1f1 Visual studio 2019
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2021/01/24 00:08
2021/01/24 00:36
2021/01/24 00:37
退会済みユーザー
2021/01/24 12:02 編集