0
votes

I am working on a method where the player can customize their sprite. I have created an array in my manager script like so:

public Sprite[] spriteImages = new Sprite[5];

In this array, I intend to add 5 png files, that will be used to update the spriteRenderer sprite. I have two problems, I am struggling accessing the array in the sprite's script (which I believe is the best way to change the image). I am also struggling to actually work out how to change the sprites image. Currently I am using this code:

this.gameObject.GetComponent<SpriteRenderer> ().sprite = GameObject.Find ("UIManager").GetComponent<UIManager> ().spriteImages [0];

In my sprite list, I have put 5 png files (2D sprite textures), but when I run the program, it says 'object reference not set to an instance of an object'. Not sure why it gives this error when the array has sprites in?

The gameobject I am referring to 'UIManager' is in a different scene to the one I am using the find from. Is this invalid?

2
Why can't you add images in the "editor" when it is static? Can you show more code or explain more of what the problem is. The error you are seeing shows you exactly how to solve the problem - you need an instance of the class not a static reference since the array is not static.pstrjds
Sorry, very new to all this, what does it mean by instance of the class?Mattattack
You need to have a UIManager object. Something like var uiMgr = new UIManager(...); in your code. That creates an instance of the class. If you are unfamiliar with those types of terms, I highly recommend you read a book or some tutorials about about object oriented programmingpstrjds

2 Answers

1
votes

The spriteImages variable is not declared as static and this means that you need an instance of UIManager to access the spriteImages variable in it. You can mark it as static and you won't need to provide instance of it to access it.

public static Sprite[] spriteImages = new Sprite[5];

That's one way to fix that error but I don't think you should use static for this. UIManager is likely a script attached to a GameObject so all you've got to do is find the GameObject the UIManager is attached to then get the UIManager component from it. That component is the script instance required to access the spriteImages variable without making it static.

private UIManager uIManager;

void Start()
{
    //Find the GameObject then the script instance
    GameObject uManagerObj = GameObject.Find("UIManager GameObject");
    uIManager = uManagerObj.GetComponent<UIManager>();

    //Now you can do this with the script instance
    this.gameObject.GetComponent<SpriteRenderer>().sprite = uIManager.spriteImages[0];
}

Make sure to replace "UIManager GameObject" from the GameObject.Find function with the name of GameObject the UIManager script is attached to.

1
votes

There are a few ways I can think of resolving your issue.

1) Singleton classes. Declare a static instance of your class in which the sprite array is kept i.e. uIManager class. You can make your class singleton using the method shown in link or else you can simply declare like this:

public class UIManager : MonoBehaviour {
    UIManager static instance;

    void Start() {
        instance = this;
    }
}

The good part is your can directly refer to your class like UIManager.instance and get all the public variables inside the class. Downside is if you have multiple scripts in same scene it will only refer to one of the instance and you will not be able to access other instances of the script which are present in scene. So this method can only be used for one instance per scene.

2) Saving a reference of your desired script in Unity Editor & by assigning it to public variable in the script. Consider you have Class A which does some calculation and you have Class B which stores some data; let's assume its Sprite array similer to your script. Now in Class A you require that sprite array for assigning. So what you can do is declare a public variable of Class B ie public ClassB classBInstance;. And you will be able to see empty the instance of ClassB field in unity editor. You can directly assign the required reference and voila you will be able to access variables of your class B.

3) Use unity API to find gameobject in which your class is attached. These are some of the API I use regularly aside from above two method:

4) Setting up your array as static, But if you do that then you will not be able to access it in unity editor and therefore wont be able to assign the Sprites in editor.

For your question I believe first method should more than do the trick.