I am working on Unity 4.7 project and have 15 UI buttons on my scene (named Button1, Button2, ...) and want to dynamically set the sprites or textures to it. I did it on next way:
public Button[] buttonsFlags = new Button[15];
...
public class TextureLoader
{
public Sprite[] textures;
public void LoadTextures(string pgNumber)
{
string path = "Flags/page" + pgNumber;
textures = Resources.LoadAll<Sprite> (path);
}
}
public void FillTheFlagsPage(string pgNumber)
{
textureLoader.LoadTextures(pgNumber);
for (int i = 1; i <= textureLoader.textures.Length; i++)
{
string btnName = "Button" + i.ToString ();
buttonsFlags[i] = GameObject.Find (btnName).GetComponent<Button>();
//buttonsFlags[i].GetComponent<Renderer>().material.mainTexture = textureLoader.textures[i];
buttonsFlags[i].image.sprite = textureLoader.textures[i];
}
}
But it seems something is wrong. I get NullReferenceException: Object reference not set to an instance of an object on line buttonsFlags[i] = GameObject.Find (btnName).GetComponent(); Please help me to find right way to do this.
Edit:
NullReferenceException: Object reference not set to an instance of an object GameController.FillTheFlagsPage (System.String pgNumber) (at Assets/Scripts/GameController.cs:327) GameController.setTransitionCountries () (at Assets/Scripts/GameController.cs:225) GameController.m__6 () (at Assets/Scripts/GameController.cs:106) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:576) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:718) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
public Button[] fifteenButtons;in your script, and simply drag the buttons to that array. Note that you're currently relying on the fact that you named the game objects correctly in the editor at dev time. That's conceptually worse than "I must remember to drag them to the array".