Unity, C#, Tutorial 001 , add an object at the run time.

August 5, 2011   ·   0 Comments

we will learn how to add a  cube object at run time.

This Tutorial will show you how to write a Hello world App in Unity3D, and understand the Unity
3D C# Language.  Please follow the step by step instructions below to create  your first Unity3D App.

new a Unity Project

make a project name “HelloWorld” in Project Directory

 

let's add an new Cube Object


you wil see a cube on the screen, now press the "Play"  button to start the game.

If you don't see any thing on the "Game" window, please update the cube
  1. press “play” button again, to become Stop status,
  2. select “Cube” on Hierarchy window
  3. update X, Y, Z become 2,0,0
 
after you update "Cube" position, you  will see the "Cube" on the Game Window.

To show you how to use C#, we add a new C# Script.
click mouse right button, and select “Create\C# Script”.

update the C# Script name to “addCubeScript”.

double-click the “addCubeScript”, the editor will show out, unity default is “MonoDevelop” app to do edition.

please change the code as shown below.

using UnityEngine;
using System.Collections;

public class addCubeScript: MonoBehaviour {
	// Update is called once per frame
	void Update () {

	}

	void Start() {
            // add a new Cube object
          GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    }
}

description:

  1. Line 4: addCubeScript: MonoBehaviour  This class name is “addCubeScript“, and Hierarchial  “MonoBehaviour”  Class
  2. Line 12:  
    void Start()  is a event, unity will call this event when Cube object Start action.
  3. Line 12:  GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube) We use  GameObject.CreatePrimitive class create a   “Cube”.
 

add "addCubeScript" to "Cube" object,  please fellow below steps,
  1. select “Cube” object, you will see the  “Cube” in Inspector windows,
  2. drag and drop the “addCubeScript” in Project to Cube,
  3.  if successful,  the “Cube” will add Cube Script as shown in the screen shot below.
because unity doesn't run your script automatically, you will need to add the script to your object,
and then the function will run when the event happens.

Finally,
press the "Play"  button to start the game. it will look like the screen shot below.


Practice:

PrimitiveType.Cube is add a cube, please change to one of below to change different object,

    • PrimitiveType.Capsule  
  • PrimitiveType.Cylinder  
  • PrimitiveType.Plane 
  • PrimitiveType.Sphere  

sample code:

please download from here, “Tutorial 001 , add an object at the run time”


shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon