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?
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.
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 ie 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:
You can find single game object with name or will return the first object with the name. GameObject.Find(string name)
This will return the game objects with a particular tag. You might want to look into setting a game objects with a tag if you don't know how to set them.
GameObject.FindGameObjectsWithTag(string tag)
This will return first game object with tag.
GameObject FindWithTag(string tag)
This API returns first gameobject of the desired type.
GameObject.FindObjectOfType(Type type)
This API will return all gamebjects with the given type.Here in both the cases the type can be of Class type. And this will return all the game objcts which has your class type attached.
GameObject.FindObjectsOfType(Type type)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.