I want to load dynamic image from external folder and set into sprite at runtime, but there is two scripts for accessing those image. When I click Button Previous or Button Next, then I call sprite variable from other script , editor show error null reference. How can I access from other script, here the scripts
LoadAssetH2H.cs
using UnityEngine;
using System.Collections;
using System.IO;
public class LoadAssetH2H : MonoBehaviour {
string[] picPathHome = new string[11];
byte[] picBytesHome;
[HideInInspector]
public Texture2D[] picTex2DHome = new Texture2D[11];
[HideInInspector]
public SpriteRenderer picSRHome;
[HideInInspector]
public Sprite[] picSPHome = new Sprite[11];
string[] picPathAway = new string[11];
byte[] picBytesAway;
[HideInInspector]
public Texture2D[] picTex2DAway = new Texture2D[11];
[HideInInspector]
public SpriteRenderer picSRAway;
[HideInInspector]
public Sprite[] picSPAway = new Sprite[11];
void Awake()
{
picSRHome = GameObject.Find ("PlayerHomePic").GetComponent<SpriteRenderer> ();
picSRAway = GameObject.Find ("PlayerAwayPic").GetComponent<SpriteRenderer> ();
for (int x =0; x<11; x++)
{
picPathHome[x] = Application.dataPath + "/Head To Head/HomeP"+(x+1)+".png";
if(File.Exists(picPathHome[x]))
{
picBytesHome = File.ReadAllBytes(picPathHome[x]);
picTex2DHome[x] = new Texture2D(256,256);
picTex2DHome[x].LoadImage(picBytesHome);
picSPHome [x] = Sprite.Create (picTex2DHome [x], new Rect (0, 0, picTex2DHome [x].width, picTex2DHome [x].height), new Vector2 (0, 0));
}
picPathAway[x] = Application.dataPath + "/Head To Head/AwayP"+(x+1)+".png";
if(File.Exists(picPathHome[x]))
{
picBytesAway = File.ReadAllBytes(picPathAway[x]);
picTex2DAway[x] = new Texture2D(256,256);
picTex2DAway[x].LoadImage(picBytesAway);
picSPAway [x] = Sprite.Create (picTex2DAway [x], new Rect (0, 0, picTex2DAway [x].width, picTex2DAway [x].height), new Vector2 (0, 0));
}
}
picSRHome.sprite = picSPHome [0];
picSRAway.sprite = picSPAway [0];
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Button.cs
using UnityEngine;
using System.Collections;
public class Button : MonoBehaviour {
LoadAssetH2H loadAssetH2H;
int counterH2H = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseUp()
{
if (this.gameObject.name == "BtnStanding")
{
}
if (this.gameObject.name == "BtnHeadToHead")
{
}
if (this.gameObject.name == "BtnStatistic")
{
}
if (this.gameObject.name == "BtnFormation")
{
}
if (this.gameObject.name == "BtnPrev")
{
if(counterH2H>1)
{
counterH2H--;
loadAssetH2H.picSRHome .sprite = loadAssetH2H.picSPHome [counterH2H];
loadAssetH2H.picSRAway .sprite = loadAssetH2H.picSPAway [counterH2H];
}
}
if (this.gameObject.name == "BtnNext")
{
if(counterH2H<11)
{
counterH2H++;
loadAssetH2H.picSRHome .sprite = loadAssetH2H.picSPHome [counterH2H];
loadAssetH2H.picSRAway .sprite = loadAssetH2H.picSPAway [counterH2H];
}
}
}
}
I would appreciate any suggestion. Thank you!