unity3d - NullReferenceException UnityEngine.MonoBehaviour.S
本文关键字:MonoBehaviour UnityEngine NullReferenceException unity3d | 更新日期: 2023-09-27 18:17:13
我想通过c#脚本改变GameObject的精灵。图片URL我成功地从一个REST API调用。对于WWW,我想为GameObject创建新的Sprite。但现在我不知道我做错了什么。我读到通过使用https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html来解决这个问题,但具体如何?
这是一个按钮的OnClick事件脚本: ApiClient apiclient = new ApiClient();
string imageurl = apiclient.callSearchApiWithCSharp(realEstateType, preisText.text, zimmerText.text);
Debug.Log(imageurl);
apiclient.loadPic(imageurl);
方法获取图片并创建它的精灵:
public void loadPic(string url)
{
WWW www = new WWW(url);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
// check for errors
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);
image.GetComponent<Image>().overrideSprite = Sprite.Create(www.texture, new Rect(0, 0, 438, 441), new Vector2(0.5f, 0.5f));
}
else
{
Debug.Log("WWW Error: " + www.error);
}
}
输出是:
https://s@sandboxpicis24-a.akamaihd.net/pic/orig02/L/106/539/941/106539941-0.png
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineMonoBehaviourBindings.gen.cs:61)
ApiClient.loadPic (System.String url) (at Assets/ApiClient.cs:89)
ObjectTypeListener.callObjectType () (at Assets/ObjectTypeListener.cs:59)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
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.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
ControllerTrack2:ProcessTrigger(PointerEventData, GameObject) (at Assets/ControllerTrack2.cs:77)
ControllerTrack2:Process() (at Assets/ControllerTrack2.cs:49)
UnityEngine.EventSystems.EventSystem:Update()
尝试使ApiClient类依赖于MonoBehaviour,并将其作为场景中对象的组件,或者直接调用Coroutine