简体   繁体   中英

unity3d - NullReferenceException UnityEngine.MonoBehaviour.StartCoroutine

I want to change the Sprite of a GameObject via C# script. The picture URL I get successfully from a REST API Call. With WWW I want to create the new Sprite for the GameObject . But now I don't have no idea what i am doing wrong. I read to solve the problem by using https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html but how in detail?

This is the OnClick event script of a button:

    ApiClient apiclient = new ApiClient();
    string imageurl = apiclient.callSearchApiWithCSharp(realEstateType, preisText.text, zimmerText.text);
    Debug.Log(imageurl);
    apiclient.loadPic(imageurl);

methods to get the picture and create a sprite of it:

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);
    }
}

Output is:

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。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM