- Home /
Can't change Image sprite because in rebuild loop?
I've combed the internet and found almost no one else with the same issue which is odd... All I'm trying to do is programmatically change the sprite property of an Image component of a UI element. This is the code I've got:
quickAccessBar.transform.GetChild(0).GetComponent<Image>().sprite = common;
I have checked, there is a child of quickAccessBar, it has an Image component attached, and common is a sprite variable and references a texture that is imported as a Sprite. That all is there. Here are the errors i'm getting:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[UnityEngine.UI.ICanvasElement].get_Item (Int32 index) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at /Applications/buildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/CanvasUpdateRegistry.cs:107)
UnityEngine.Canvas.SendWillRenderCanvases ()
And this second error happens for almost EVERY UI element in the scene:
Trying to remove Image (UnityEngine.UI.Image) from rebuild list while we are already inside a rebuild loop. This is not supported.
UnityEngine.UI.MaskableGraphic:OnDisable()
Any idea what's going on?
EDIT: I have tried and failed at replicating the error in another unity scene. Any idea what this rebuild list issue is?
I could not reproduce it. I Added a Canvas to the scene, added an Image so it was its child and added your code line to a script attached to the canvas. Could assign the image no problem. Using Unity 4.6 final
I just played with it some more and did error pause to stop debugging when the problem occurs, and when i clicked on the game object with the Image component attached to it, the inspector view was blank. It looks like the game object is getting destroyed or something? I have combed my code and i don't reference that game object ANYWHERE else
Answer by kkiniaes · Dec 10, 2014 at 02:20 AM
Figured it out. For some reason I just needed to set the public sprite that i'm changing to (the one called "common") to "none", and then drag the sprite I want in to the public field again. For some reason Unity thought it was a Texture2D, not a sprite. Even though I had set the import settings to Sprite.
Your answer
Follow this Question
Related Questions
Color blending in UI sprite 1 Answer
Make images fit in UI 0 Answers
Unity UI not showing some Images 0 Answers
Change UI GUI Image Sprite at runtime .. Unity 5 .. js 1 Answer
Access sprite secondary texture in shader for UI canvas image 1 Answer