hello world: your first programbook.prototools.net/wp-content/uploads/2014/06... · hello world?!...

92
HELLO WORLD: YOUR FIRST PROGRAM CHAPTER 18 1

Upload: others

Post on 19-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

HELLO WORLD: YOUR FIRST PROGRAM

CHAPTER 18

1

Page 2: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics

2

Page 3: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

2

Page 4: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project

2

Page 5: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

2

Page 6: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

§ MonoDevelop: Unity's Code Editor

2

Page 7: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

§ MonoDevelop: Unity's Code Editor

§ Attaching Scripts to GameObjects

2

Page 8: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

§ MonoDevelop: Unity's Code Editor

§ Attaching Scripts to GameObjects

§ Start() and Update()

2

Page 9: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

§ MonoDevelop: Unity's Code Editor

§ Attaching Scripts to GameObjects

§ Start() and Update()

§ GameObject Prefabs and Instantiation

2

Page 10: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Topics§ Hello World?

§ Creating a Unity Project– The Unity Project Folder

§ MonoDevelop: Unity's Code Editor

§ Attaching Scripts to GameObjects

§ Start() and Update()

§ GameObject Prefabs and Instantiation

§ The HelloWorld Project

2

Page 11: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?

3

Page 12: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?§ Hello World is often the first program written by

anyone learning a new programming language.

3

Page 13: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?§ Hello World is often the first program written by

anyone learning a new programming language.

§ Outputs "Hello World!" to the Console

3

Page 14: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?§ Hello World is often the first program written by

anyone learning a new programming language.

§ Outputs "Hello World!" to the Console

3

Page 15: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?

4

Page 16: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?§ The code of HelloWorld.cs is very simple:

4

Page 17: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Hello World?§ The code of HelloWorld.cs is very simple:

4

Page 18: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project

5

Page 19: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

5

Page 20: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder

5

Page 21: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

5

Page 22: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button

5

Page 23: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location

5

Page 24: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field

5

Page 25: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

5

Page 26: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

– Windows

5

Page 27: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

– Windows• Click the Browse… button

5

Page 28: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

– Windows• Click the Browse… button• Navigate to the right location

5

Page 29: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

– Windows• Click the Browse… button• Navigate to the right location• Click the New Folder button and give the new folder the name of

your project.

5

Page 30: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ From the menu bar, choose File > New Project…

§ Choose the location for your project folder– Mac OS X

• Click the Set… button• Navigate to the right location• Type the project name into the Save As field• Click the Save button

– Windows• Click the Browse… button• Navigate to the right location• Click the New Folder button and give the new folder the name of

your project.• Click the Select Folder button

5

Page 31: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project

6

Page 32: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ Set up defaults for 3D

6

Page 33: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ Set up defaults for 3D

§ Click the Create Project or Create button

6

Page 34: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ Set up defaults for 3D

§ Click the Create Project or Create button

§ Appendix A contains detailed instructions

6

Page 35: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project

7

Page 36: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ The Project pane shows the contents of the Assets

folder inside your Unity project folder

7

Page 37: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ The Project pane shows the contents of the Assets

folder inside your Unity project folder– Right-click in the Project pane and choose Reveal in Finder

(or Show in Explorer) from the pop-up menu

7

Page 38: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ The Project pane shows the contents of the Assets

folder inside your Unity project folder– Right-click in the Project pane and choose Reveal in Finder

(or Show in Explorer) from the pop-up menu

7

Page 39: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Creating a Unity Project§ The Project pane shows the contents of the Assets

folder inside your Unity project folder– Right-click in the Project pane and choose Reveal in Finder

(or Show in Explorer) from the pop-up menu

7

Page 40: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor

8

Page 41: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

8

Page 42: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

8

Page 43: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

8

Page 44: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

– This will launch MonoDevelop

8

Page 45: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

– This will launch MonoDevelop– Though the launch process takes some time

8

Page 46: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

– This will launch MonoDevelop– Though the launch process takes some time

§ You must save a document in MonoDevelop for it to recompile and update in Unity

8

Page 47: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

– This will launch MonoDevelop– Though the launch process takes some time

§ You must save a document in MonoDevelop for it to recompile and update in Unity

§ On Windows, Microsoft Visual Studio may be used

8

Page 48: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor§ Unity uses MonoDevelop for code editing

– MonoDevelop is a separate program developed by a different team

§ To open MonoDevelop, double-click any C# script in your Project pane

– This will launch MonoDevelop– Though the launch process takes some time

§ You must save a document in MonoDevelop for it to recompile and update in Unity

§ On Windows, Microsoft Visual Studio may be used– Instructions for this can be found online

8

Page 49: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor

The MonoDevelop Window9

Page 50: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor

The MonoDevelop Window9

Code Pane

Page 51: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor

The MonoDevelop Window9

Code Pane

Classes Pane

Page 52: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

MonoDevelop: Unity's Code Editor

The MonoDevelop Window9

Code Pane

Classes Pane

DocumentOutline Pane

Page 53: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Attaching Scripts to GameObjects

10

To work in Unity, a C# script must be attached to a GameObject

Page 54: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Attaching Scripts to GameObjects

11

This makes the script a component of the GameObject

Page 55: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()

12

Page 56: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project

12

Page 57: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

12

Page 58: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once

12

Page 59: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

12

Page 60: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}

12

Page 61: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame

12

Page 62: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame• This can happen over 200 times per second!

12

Page 63: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame• This can happen over 200 times per second!

– void Awake() {…} (not used in HelloWorld, but important)

12

Page 64: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame• This can happen over 200 times per second!

– void Awake() {…} (not used in HelloWorld, but important)• Called once

12

Page 65: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame• This can happen over 200 times per second!

– void Awake() {…} (not used in HelloWorld, but important)• Called once• Called at the moment the GameObject is created

12

Page 66: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Start() and Update()§ You make use of Start() and Update() in the

HelloWorld project– void Start() {…}

• Called once• Called immediately before the first Update() is called

– void Update() {…}• Called every frame• This can happen over 200 times per second!

– void Awake() {…} (not used in HelloWorld, but important)• Called once• Called at the moment the GameObject is created• Guaranteed to be called before Start()

12

Page 67: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation

13

Page 68: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made

13

Page 69: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane

13

Page 70: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane– Can be assigned to a script variable in the Inspector pane

13

Page 71: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane– Can be assigned to a script variable in the Inspector pane

•public GameObject gameObjectPrefab;

13

Page 72: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane– Can be assigned to a script variable in the Inspector pane

•public GameObject gameObjectPrefab;

– Then, an instance of the prefab can be created in code

13

Page 73: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane– Can be assigned to a script variable in the Inspector pane

•public GameObject gameObjectPrefab;

– Then, an instance of the prefab can be created in code•Instantiate( gameObjectPrefab );

13

Page 74: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

GameObject Prefabs and Instantiation§ A prefab is a mold from which GameObject instances

can be made– Created by dragging a GameObject from the Hierarchy pane

into the Project pane– Can be assigned to a script variable in the Inspector pane

•public GameObject gameObjectPrefab;

– Then, an instance of the prefab can be created in code•Instantiate( gameObjectPrefab );

§ This is used in HelloWorld to create thousands of instances of a Cube GameObject prefab

13

Page 75: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project

14

Page 76: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

14

Page 77: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()

14

Page 78: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

14

Page 79: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

14

Page 80: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

§ Instantiate an instance of the Cube prefab

14

Page 81: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

§ Instantiate an instance of the Cube prefab– Once using Start()

14

Page 82: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

§ Instantiate an instance of the Cube prefab– Once using Start()– Many times using Update()

14

Page 83: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

§ Instantiate an instance of the Cube prefab– Once using Start()– Many times using Update()

• This will create a cascade of thousands of Cube instances

14

Page 84: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project§ Output "Hello World!" to the Console pane

– Once using Start()– Many times using Update()

§ Create a Cube prefab that reacts to gravity & physics

§ Instantiate an instance of the Cube prefab– Once using Start()– Many times using Update()

• This will create a cascade of thousands of Cube instances

– Over other physically-modeled objects

14

Page 85: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

The HelloWorld Project

15

The final HelloWorld scene

Page 86: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary

16

Page 87: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

16

Page 88: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

§ Unity projects are stored as many files in project folders on your hard drive

16

Page 89: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

§ Unity projects are stored as many files in project folders on your hard drive

§ MonoDevelop is used to edit code for Unity

16

Page 90: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

§ Unity projects are stored as many files in project folders on your hard drive

§ MonoDevelop is used to edit code for Unity

§ Scripts must be attached to GameObjects to run

16

Page 91: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

§ Unity projects are stored as many files in project folders on your hard drive

§ MonoDevelop is used to edit code for Unity

§ Scripts must be attached to GameObjects to run

§ Start(), Update(), and Awake() are called at different times and have different uses

16

Page 92: HELLO WORLD: YOUR FIRST PROGRAMbook.prototools.net/wp-content/uploads/2014/06... · Hello World?! Creating a Unity Project – The Unity Project Folder! MonoDevelop: Unity's Code

Chapter 18 – Summary§ Hello World is a common first program to make in

any new language

§ Unity projects are stored as many files in project folders on your hard drive

§ MonoDevelop is used to edit code for Unity

§ Scripts must be attached to GameObjects to run

§ Start(), Update(), and Awake() are called at different times and have different uses

§ GameObject prefabs can be instantiated many times

16