Hey there,
in my game there is a part where you can put virtual stickers onto a background.
On the bottom there is a menu with all the available stickers.
This menu is filled at runtime. It's done by initializing a prefab and assigning a sprite to the SpriteRenderer component. All of the Sprites are in one atlas.
It works sometimes, but in most cases the editor crashes.
Here is the Code.
In the object, wich loads and holds the stickers:
private void fillWithStickers (int tab)
{
float spaceBetweenStickers = 0.2f;
float nextStickerPosition = spaceBetweenStickers;
for (int i = 1; i <= numbersOfStickers[tab]; i++) {
GameObject miniSticker = Instantiate (Resources.Load ("Prefabs/Sticker/MiniSticker", typeof(GameObject))) as GameObject;
miniSticker.GetComponent ().loadSprite ("Sprites/Sticker/" + stickerNamePrefixes [tab] + "_mini", stickerNamePrefixes [tab] + "_" + i);
float stickerWidth = miniSticker.GetComponent ().bounds.size.x;
miniSticker.transform.SetParent (transform);
miniSticker.transform.localPosition = new Vector2 (nextStickerPosition + (stickerWidth / 2.0f), 0);
nextStickerPosition += spaceBetweenStickers + stickerWidth;
}
GetComponent ().SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, nextStickerPosition);
float camHalfWidth = Camera.main.aspect * Camera.main.orthographicSize;
maxX = (GetComponent ().rect.size.x / 2) - camHalfWidth;
minX = -maxX;
transform.position = new Vector3 (maxX, transform.position.y);
}
That's what the AutoSDSprites class is doing:
public void loadSprite (string atlasPath, string spriteName)
{
atlasPath = applySDHDSuffix (atlasPath);
GetComponent ().sprite = getSpriteFromSheet (Resources.LoadAll (atlasPath), spriteName);
Resources.UnloadUnusedAssets ();
}
private Sprite getSpriteFromSheet (Sprite[] sprites, string spriteName)
{
foreach (Sprite s in sprites) {
if (s.name == spriteName) {
return s;
}
}
return null;
}
So what can I do? Where could the problem be?
**EDIT:**
I found that the editor spits out this into the log, right before it crashes. Not just once, but a lot.
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.Update () (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1377)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:185)
UnityEditor.HostView.Invoke (System.String methodName) (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:178)
UnityEditor.HostView.SendUpdate () (at /Users/builduser/buildslave/unity/build/Editor/Mono/HostView.cs:243)
UnityEditor.EditorApplication.Internal_CallUpdateFunctions () (at /Users/builduser/buildslave/unity/build/artifacts/generated/common/editor/EditorApplicationBindings.gen.cs:264)
and this is thrown occasionally between the other
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.InspectorWindow.ShouldCullEditor (UnityEditor.Editor[] editors, Int32 editorIndex) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1225)
UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:962)
UnityEditor.InspectorWindow.OnGUI () (at /Users/builduser/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:350)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
**EDIT2:**
I could replicate, when it crashes and when it doesn't.
It crashes, if I have the GameObject with the `fillWithStickers(int tab)` method selected in the editor, when I run the game.
I can live with that but it is sure annoying, if it crashes if I select the wrong item...
↧