Search Unity

NullReferenceException: Object reference not set to an instance of an object pictureFactory.CreateIm

Discussion in 'General Discussion' started by zkarkafi, Aug 24, 2022.

  1. zkarkafi

    zkarkafi

    Joined:
    May 11, 2022
    Posts:
    3
    Hi guys,
    i am new to unity and AR and for my thesis im trying to build an AR google Museum using unity 3D and vuforia ground plane stage where i project images from google using rest protocoll and google custom searh api and projecting the pictures with vuforia fround plane stage.

    I get this error :
    NullReferenceException: Object reference not set to an instance of an object
    pictureFactory.CreateImages (System.Collections.Generic.List`1[T] urlList, System.Int32 resultNum, UnityEngine.Vector3 camForward) (at Assets/scripts/pictureFactory.cs:35)
    GoogleService+<<GetPicture>g__PictureRoutine|3_0>d.MoveNext () (at Assets/scripts/GoogleService.cs:51)


    This is Google service script where the function is called:

    Vector3 cameraForward = Camera.main.transform.forward;

    int RowNum = 1;
    for( int i = 0 ; i <= 9 ; i=i+2) {

    string url= "https://customsearch.googleapis.com/customsearch/v1?cx=c52e7231e56d59bd3&exactTerms=" + query +
    "&filter=1&num=10&searchType=image&start=" + i + "&fields=items%2Flink&key=" + API_KEY;


    UnityWebRequest reg = new UnityWebRequest(url);
    reg.downloadHandler = new DownloadHandlerBuffer();

    yield return reg.SendWebRequest();

    Debug.Log(reg.downloadHandler.text);

    List<string> links= ParseResponse(reg.downloadHandler.text);
    Debug.Log(links[9]);
    pictureFactory.CreateImages(ParseResponse(reg.downloadHandler.text) , RowNum, cameraForward);
    RowNum++;
    }


    This is my Createimages function:


    public void CreateImages(List <string> urlList, int resultNum , Vector3 camForward) {
    int PicNum=1;
    Vector3 center = Camera.main.transform.position;
    foreach (string url in urlList){
    Vector3 pos= GetPosition (PicNum, resultNum, camForward);
    GameObject pic= Instantiate(PicPrefab , pos , Quaternion.identity ,this.transform);
    pic.GetComponent<pictureBehaviour>().LoadImage(url);
    PicNum++;
    }


    the LoadImage function is:

    StartCoroutine(LoadImageFromURL (url));

    }
    IEnumerator LoadImageFromURL(string url) {

    using (UnityWebRequest loaded = UnityWebRequestTexture.GetTexture(url))
    {
    yield return loaded.SendWebRequest();

    if (loaded.result != UnityWebRequest.Result.Success)
    {
    Debug.Log(loaded.error);
    }
    else
    {
    Debug.Log(url);

    quadRenderer.material.mainTexture = DownloadHandlerTexture.GetContent(loaded);

    }
    }


    I dont really understand what parameter is not taken by the function or is wrong can someone please help.