Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created January 20, 2021 08:59
Show Gist options
  • Save todorok1/452dc92acde611aa5628a03924afb69d to your computer and use it in GitHub Desktop.
Save todorok1/452dc92acde611aa5628a03924afb69d to your computer and use it in GitHub Desktop.
SetParentのサンプル
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <Summary>
/// 指定した数のゲームオブジェクトをインスタンス化するスクリプトです。
/// </Summary>
public class ObjectMaker : MonoBehaviour
{
// インスタンス化するPrefabオブジェクトをアサインします。
public GameObject prefab;
// 親オブジェクトのトランスフォームをアサインします。
public Transform parentTran;
// ゲームオブジェクトを生成する数を指定します。
public int prefabNum;
void Start()
{
InstantiatePrefab();
}
/// <Summary>
/// ゲームオブジェクトをPrefabから作成する処理です。
/// </Summary>
void InstantiatePrefab()
{
for (int i = 0; i < prefabNum; i++)
{
GameObject obj = Instantiate(prefab, Vector3.zero, Quaternion.identity);
obj.transform.SetParent(parentTran);
obj.transform.position = new Vector3(i, 0f, 0f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment