Intances with Public Static, or at least somehting similar
Hio hey i have this situation. I Have this Array with Sprites, and i want to Isntantiate Buttons that show the Sprites of that array but when try to access it it throw the classic CS0176, try to make it access directly to the script, but didn't work so there is a way to fix it or at least another method here are the scripts:
The script with the public static Member
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Sprite_Change : MonoBehaviour {
public static Sprite mySprite; //Member that want to access
Image actual_Sprite;
public GameObject[] target_Sprite;
public string parte;
public static int ID_Sprite;
void Start ()
{
target_Sprite = GameObject.FindGameObjectsWithTag(parte);
actual_Sprite = GetComponent<Image>();
actual_Sprite.sprite = mySprite;
}
public void IsPressed ()
{
SpriteChange();
}
void SpriteChange()
{
for(int x=0;x<target_Sprite.Length;x++)
{
target_Sprite[x].GetComponent<SpriteRenderer>().sprite = mySprite;
}
}
}
and the script that want to access that script:
using UnityEngine;
using System.Collections;
public class LoadSprites : MonoBehaviour {
public string Paths;
public static Sprite[] lista_Sprites;
public Sprite_Change Boton;
void Awake ()
{
lista_Sprites = Resources.LoadAll<Sprite>(Paths);
for (int x = 0; x < lista_Sprites.Length; x++)
{
Instantiate(Boton, transform);
Boton.mySprite = lista_Sprites[x]; //Line that access the script
}
}
}
Answer by HenryStrattonFW · Feb 11, 2017 at 11:58 AM
You are talking about a static member variable "mySprite" but you are then trying to access it via an instance reference. You should either be accessing it like this.
Sprite_Change.mySprite = lista_Sprites[x]; // Note accessing via the type.
Or access as you were previously but make the mySprite variable non-static. (Unless you need it to be static for other reasons, in which case use the first method).
Your answer
![](https://koobas.hobune.stream/wayback/20220612110809im_/https://answers.unity.com/themes/thub/images/avi.jpg)