LoginSignup
3
1

More than 3 years have passed since last update.

Unityでスクリプトを作成し、Hello, Worldを表示する

Last updated at Posted at 2020-07-10

やりたいこと

Unity上で実行ボタンを押すと、Hello, Worldが表示されるスクリプトを作成します。
Unityの画面構成、マウス操作、C#の基本などは以下の投稿を参考にしてください。
Unityとc#の超入門 ~基礎編~

流れ

  1. オブジェクトを作成
  2. スクリプトを作成
  3. ゲームオブジェクトを作成
  4. スクリプトをアタッチ
  5. スクリプトを編集
  6. Hello Worldを表示

詳細

1. オブジェクトを作成

HierarchyウィンドウのCreate -> 3D Object -> Cube の順にクリックして、立方体のオブジェクトを作成します。
image.png

2. スクリプトを作成

Projectウィンドウ内で右クリックし、Create -> C# Scriptを選択します。Scriptの名称をtestに変更します。
image.png

3. ゲームオブジェクトを作成

スクリプトを動かすには、何らかのゲームオブジェクトと結びつける(アタッチする)必要があります。作成したスクリプトを動かすためにゲームオブジェクトを追加します。
HierarchyウィンドウのCreate -> Create Emptyを選択します。
image.png

4. スクリプトをアタッチ

testスクリプトを、HierarchyウィンドウのGameObjectにドラッグ&ドロップします。これで、スクリプトをゲームオブジェクトに結び付ける(アタッチする)ことができました。
image.png

5. スクリプトを編集

testスクリプトをダブルクリックすると、Visual Studioが起動し、スクリプトを編集できるようにになります。Hello, Worldと表示されるよう10行目にソースコードを追記します。Visual Studio上でスクリプトを保存します。
image.png

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello, World");
    }

    // Update is called once per frame
    void Update()
    {

    }
}

6. Hello Worldを表示

Unityに戻り、SceneビューにあるPlayボタンを押します。ConsoleウィンドウにHello, Worldが表示されました。
image.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1