LoginSignup
1
0

More than 1 year has passed since last update.

Instantiate前のGameObjectへのAddComponentしてみる

Last updated at Posted at 2018-09-17

概要

Instantiateする前のGameObject
(例えばResourcesフォルダ以下にあるPrefabをResources.Loadした状態)
にAddComponentしてみる

やってみる

Resources以下に空のTest.prefabを配置し、以下のコードでやってみる

using UnityEngine;

public class Hoge : MonoBehaviour
{
    void Start ()
    {
        var go = Resources.Load("Test") as GameObject;
        go.AddComponent<Fuga>();
    }
}

結果

元のプレハブにAddComponentされた
スクリーンショット 2018-09-17 19.06.29.png
多分、やってはいけない

補足 - 2021/12/11

ここではRuntime上でAddComponentしているので、まあよろしくないと思いますが、
Editorコードとして書く分には有用な場合もあるので、必ずしもやってはいけない。ということも無い。

1
0
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
1
0