首页 > 解决方案 > Unity Class Instantiate C#

问题描述

Working on an assignment and this is one of the examples of code that I'm trying to work with in my code.

GameObject prefabRocks;
GameObject rocks = Instantiate(prefabRocks) as GameObject;

I get an error every time this code runs in Unity.

Error Is: UnassignedReferenceException: The variable prefabRocks of Class1 has not been assigned. You probably need to assign the prefabRocks variable of the Class1 script in the inspector.

Can someone help me understand what exactly this code does?

It would help if you can also point me to some reading I can do to understand this weird way of instantiating classes and methods.

标签: c#classunity3dinstantiation

解决方案


你的这个prefabRocks变量是未分配的。它里面没有任何东西,你必须用一些东西来初始化它。您可以从资源文件夹中加载预制件,Start()Awake()可以将其设为公共变量并通过将预制件从项目选项卡拖放到此处的插槽中来分配给变量

在此处输入图像描述


推荐阅读