我创建了一个名为Maps的空对象,并创建了一个子and映像。此脚本分配给Maps,子rawimage对象已分配给检查器中的rawimage变量。
当我播放时,检查器没有显示指定的set映像,并说“对象引用没有设置为对象的实例”。
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class GoogleAPI : MonoBehaviour {
public string url;
public RawImage img;
public float lon = 3.7533f;
public float lat = 53.3047f;
public int zoom;
public int mapHeight;
public int mapWidth;
public int scale;
LocationInfo li;
public enum mapType { roadMap, satelite, hybrid, terrain };
public mapType mapSelected;
private IEnumerator Map() {
url = "https://maps.googleapis.com/maps/api/staticmap?center=" + lat + "," + lon +
"&zoom=" + zoom + "&size=" + mapHeight + "x" + mapWidth + "&Scale=" + scale
+ "&maptype=" + mapSelected +
"&markers=color:blue%7Clabel:S%7C40.702147,-74.015794&markers=color:green%7Clabel:G%7C40.711614," +
"-74.012318&markers=color:red%7Clabel:C%7C40.718217,-73.998284&key=AIzaSyDh1_nS-l7nWOFWvt0Gg9-9dY_11qWzK_Q";
WWW www = new WWW(url);
yield return www;
img.texture = www.texture;
img.SetNativeSize();
}
private void Start() {
img = gameObject.GetComponent<RawImage>();
StartCoroutine(Map());
}
}发布于 2018-04-27 18:04:59
在您发布的代码中,您从未将任何东西分配给img。因此,它是null,它导致“对象引用未设置为对象实例”错误。
如果你在线路上放了一个断点,
img.texture = www.texture;您可能会看到img是null。
您需要在某个地方创建一个RawImage,并将其分配给img字段。
https://stackoverflow.com/questions/49490672
复制相似问题