[英]How to create objects and add them to scenes using a script?
I have a scene and one object, I want to write a script which will create multiples that object with different transform.position
The object is defined in the folder _prefabs
.我有一个场景和一个对象,我想编写一个脚本,该脚本将使用不同的
transform.position
创建多个该对象。该对象在文件夹_prefabs
定义。
This is what I have so far, but it isn't working.这是我到目前为止所拥有的,但它不起作用。
void (int count)
{
while(count--)
{
var x = someFunctionToCreateObject();
x.transform.position = someFunction();
scene.add(x);
}
}
You just need to google it.你只需要谷歌一下。
https://answers.unity.com/questions/784642/is-it-possible-to-use-script-to-create-objects-in.html https://answers.unity.com/questions/784642/is-it-possible-to-use-script-to-create-objects-in.html
GameObject go = new GameObject("objectName");
So you just create it and it gets added to the scene but the location and other things are your responsibility.所以你只需创建它并将它添加到场景中,但位置和其他事情是你的责任。
Just keep a ref to your prefab and instantiate at random positions;只需保留对您的预制件的引用并在随机位置实例化;
public GameObject yourPrefabRef;
public void CreateObjects(int objectCount)
{
for (int count = 0; count < objectCount; count++)
{
GameObject newObject = Instantiate (yourPrefabRef);
newObject.transform.position = someFunction();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.