1 Replies - 8052 Views - Last Post: 22 October 2011 - 04:12 PM Rate Topic: -----

#1 Aramyth  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 51
  • Joined: 14-January 10

Unity Creating Game Objects Programmatically

Posted 04 October 2011 - 09:06 PM

Hi all,

I couldn't find a specific section dedicated to Unity or Game Engines in general, so I assume this was the best section for this. If not, I'm sorry!

I've recently been starting to learn Unity, however, I haven't been able to figure out how to create Game Objects with a script with C# in unity.

What I've tried so far is creating an empty game object, then attaching my script and rigid body physics to that object.
 //C#
GameObject bulletClone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;


But that doesn't work. I found a tutorial with how to do it with Javascript (not ideal but would be sufficient), however, once I create the objects they go spinning into oblivion.

//In Javascript
var thePrefab : GameObject;
 
function Start () {

        for (i=0;i<=10;i++) {
            var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
            instance.transform.position.x += 10;
         }

}


If anyone has any ideas, or any extra information to help me out, I'd appreciate it. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Unity Creating Game Objects Programmatically

#2 kabilah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-April 10

Re: Unity Creating Game Objects Programmatically

Posted 22 October 2011 - 04:12 PM

Does your game object fall through the terrain?

If it does, increase the y value in the vector3 in its starting location so that it's sitting just on top of the terrain. Unity's really finicky about breaking the terrain plain when gravity is applied to an object.

If it's literally spinning and flying off, I'd have to look at the other code attached to the object. It's probably a calculation issue with transform.position of the object in an update function.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1