Suggest a change
Success!
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
Close
Sumbission failed
For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
Close
public function
GetComponent(
type:
Type):
Component;
public
def
GetComponent(
type as Type)
as
Component
Description
ゲームオブジェクトに type
がアタッチされている場合は type
のタイプを使用してコンポーネントを返します。ない場合はnullです
GetComponentは、他のコンポーネントにアクセスするための主力となる方法です。javascriptではスクリプトの型はプロジェクトビューで見えるスクリプトの名前になります。この関数でビルドインのコンポーネントやスクリプトにアクセスすることが可能です。
// Disable the spring on the HingeJoint component.
var hinge : HingeJoint;
hinge = gameObject.GetComponent(HingeJoint);
hinge.useSpring = false;
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public HingeJoint hinge;
void Example() {
hinge = gameObject.GetComponent<HingeJoint>();
hinge.useSpring = false;
}
}
import UnityEngine
import System.Collections
public class ExampleClass(MonoBehaviour):
public hinge as HingeJoint
def Example() as void:
hinge = gameObject.GetComponent[of HingeJoint]()
hinge.useSpring = false
public function GetComponent():
T;
public T GetComponent();
public
def GetComponent()
as T
public function
GetComponent(
type:
string):
Component;
public
def
GetComponent(
type as string)
as
Component
Description
ゲームオブジェクトに type
がアタッチされている場合は type
の名前を使用してコンポーネントを返します。ない場合はnullです
パフォーマンス上の理由から文字列ではなくタイプでGetComponentを実行することをお勧めします。
時々JavascriptからC#スクリプトにアクセスしようとするとタイプでコンポーネントを取得できない可能性があります。
この場合、文字列を使ってコンポーネントを取得するようにしてください。
// Disable the spring on the HingeJoint component.
var hinge : HingeJoint;
hinge = gameObject.GetComponent("HingeJoint");
hinge.useSpring = false;
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public HingeJoint hinge;
void Example() {
hinge = gameObject.GetComponent("HingeJoint") as HingeJoint;
hinge.useSpring = false;
}
}
import UnityEngine
import System.Collections
public class ExampleClass(MonoBehaviour):
public hinge as HingeJoint
def Example() as void:
hinge = (gameObject.GetComponent('HingeJoint') as HingeJoint)
hinge.useSpring = false