unityにてUIのPanelにテクスチャをスクリプトから指定して貼りたいと思ったのですが、うまくいきませんでした
自分でいろいろ調べたのですがよくわからず、とりあえず下記のサイトを参考にやっては見ました
https://dennou-note.blogspot.com/2014/01/unity_20.html
私はunityを始めて日が浅く、C#もその時初めて触ったぐらいなのでまだよく書き方がわかっていない状態です
一応自分で書いてみたプログラムが以下のものです
C#
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6 7public class TextureSet : MonoBehaviour 8{ 9 10 const string TEXNAME_SEN1 = "Texture/j_seikai01"; 11 12 private void Start() 13 { 14 Texture2D J_SEIKAI01; 15 J_SEIKAI01 = Resources.Load(TEXNAME_SEN1) as Texture2D; 16 GameObject go = GameObject.Find("Panel"); 17 if (go) 18 { 19 Debug.Log(go.name); 20 } 21 else 22 { 23 Debug.Log("No game object called wibble found"); 24 } 25 go.GetComponent<Renderer>().material.mainTexture = J_SEIKAI01; 26 } 27 28 // Update is called once per frame 29 void Update() 30 { 31 32 } 33}
実行してみると
NullReferenceException: Object reference not set to an instance of an object....
というエラーが出たので参照がうまくできていないんだと思うのですがどこがおかしいのかがわかりません
(エラー文で「go.Get...」のところがだめだとは言われたのですが何がだめかわかっていない状態です)
わかる方がいらっしゃいましたら教えていただけると幸いです
作成環境
unity 2017.4.2f2
VisualStudio 2017
回答3件
あなたの回答
tips
プレビュー