詳細: Unity3d の C# で Unity のドキュメントを検索しましたが、この例は私の場合には当てはまりません。訪問した他の Web サイトと同じです。(必要に応じてそれらをリストしませんでした。)
私がやろうとしているのは、クラス変数をコンストラクターでコンポーネントに渡すことです:
CustomComponentClass ccc = new CustomComponentClass(2343, "blop");
gameObject.AddComponent(ccc);
私がしようとしていることがうまくいくかどうか、または何かを逃したかどうかを知りたい...
問題:
gameObject.AddComponent(ccc); -> cannot convert ccc to string.
gameObject.AddComponent<ccc>(); -> Are you missing a using directive or an assembly reference? (i'm not, i'm using a variable and all is in the same namespace)
gameObject.AddComponent<CustomComponentClass>(ccc); -> UnityEngine.GameObject.AddComponent(string) cannot be used with the type arguments
gameObject.AddComponent<CustomComponentClass>(2343, "blop"); -> No overload for method AddComponent takes 2 arguments
何を試してもうまくいきません。C# は実際には JavaScript とは異なります。私が正しければ、以前は js の addcomponent で変数を渡すことができました。
コンテキスト: コンストラクターで絶対に渡す必要があります。プライベートなフィールドが 2 つありますが、安全上の理由から、静的、定数、またはパブリックにすることはできません。
オプション: 答えが複雑すぎる場合は、おそらく get/set を作成し、フィールドが空かどうかを確認します。