Dynamic game objects

Hi,

Hope this is in the correct thread? I’m new to unity but not C#. Was wanting to create dynamic game objects and add them to a scene, do you use instantiate for this and do you have to attach to a prefab?

Any help is much appreciated.

Thanks

for the most part, yes. Although there is “create primitive” http://docs.unity3d.com/ScriptReference/GameObject.CreatePrimitive.html but that has a limited selection of meshes. You can also create an empty by using the GameObject’s constructor.

no, but prefabs let you add and customise components in a useful way before the instance is created, you can do this via code per the examples in the GameObject constructor scripting reference
http://docs.unity3d.com/ScriptReference/GameObject-ctor.html

Many thanks - I guess creating them dynamically is the way to go to do a path of blocks, a little like ‘Temple run’ ?

usually in such games the coins/things spin/bounce/are enticing in some way, and they’ll need colliders/attributes setup to govern their interaction with the player. This is going to require scripts attached to the thing. As such you’ll probably find it easier to create a prefab and instantiate instances of that prefab as needed as the level is created (again, the level is made of prefabs with colliders/triggers/models all setup).

Unity is built around this prefab/component model approach, you can get around it, but you are then “getting around” what the engine is expecting which is likely to be more of a headache than an advantage. If you prefer the “all built in code” approach I’m sure there are other frameworks designed around that that might be more akin to that preference.

1 Like

You can create things by instantiating prefabs. This is the ‘normal’ unity way to do so. But you can build something completely in code. If I’ve not messed anything up, this code should create a red image.

GameObject newObject = new GameObject("Red Sprite");
SpriteRender renderer = newObject.AddComponent<SpriteRenderer>();
Texture2D texture = new Texture2D(256, 256);
Colour[]  colours = texture.GetPixels();
for (int i = 0; i < colour.Length, i++){
    colours[i] = Colour.red;
}
renderer.sprite = Sprite.Create(texture, new Rect (0,0,1,1), new Vector2 (0.5f, 0.5f));

Many thanks all for the replies, much appreciated. This allows me to draw 400 spheres, but can you create your type instead of using the primitive ones?

void Start()
    {
        for (int y = 0; y < 20; y++)
        {
            for (int x = 0; x < 20; x++)
            {
                GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                cube.AddComponent<Rigidbody>();
                cube.transform.position = new Vector3(x, y, 0);
            }
        }
    }

That would be called a prefab…

Thanks,

So if I have a game object with a certain material applied to it (say grass), I add this to the prefab and then use Instantiate?

Yup

Cool, must admit, wished I’d used Unity a long time ago now, all my games I’ve developed I’ve never used
an engine, always gone low level using OpenGL or Direct3D and wrote most stuff myself which takes ages and never ever going to be as good as these engines.

1 Like

One more thing that’s been confusing me, I wrote a sandbox game like terraria in Java using libGDX, of course this was 2D and I created caves using cellular automata and landscape via perlin noise, I could have massive worlds. Is something like this do-able in Unity? Would you use a mesh or something?

Thanks