我正在尝试设置一种方法将产品放入我的商店,并且在调用方法后代码不会执行。它不会抛出任何错误,它只是不执行,我无法弄清楚为什么。
继承调用AddProduct的地方
void Start()
{
try
{
db = GameObject.Find("Database").GetComponent<Database>();
dm = GameObject.Find("DispensaryManager").GetComponent<DispensaryManager>();
AddProduct();
}
catch (NullReferenceException)
{
print("Database is null");
}
}
代码在第5行之后停止运行:
public void AddProduct()
{ // This method is called one time during the Start() method of its gameobject
List<StoreObjectReference> jars = db.GetProducts(StoreObjectReference.productType.jar); // this is not null
GameObject jar = Instantiate(jars[3].gameObject_); // successfully instantiates
jar.AddComponent<Jar>();
jar.GetComponent<Jar>().product = new StorageJar(jar);
print("This prints"); // This prints
ProductPosition jarPosition = dm.dispensary.Storage_cs[0].GetRandomStorageLocation(); // Storage_cs[0] is not null
print("This doesnt print"); // Never reached
jar.transform.position = jarPosition.transform.position; // Never reached
}
继承人GetRandomStorageLocation()
public ProductPosition GetRandomStorageLocation()
{ // Does not throw any errors
print("Getting product position"); // This prints
return storageShelves[0].positions[0]; // This does exist, I am only hardcoding the two indexes for testing purposes
}
执行代码后,这是控制台的screenshot
这是ProductPosition
public class ProductPosition : MonoBehaviour
{
public Product product; // the product stored here
public Vector3 positon; // position for products stored here
}