Version: 5.4
public Component GetComponent (Type type);

Parámetros

type El tipo de Component para recuperar.

Descripción

Devuelve un componente de tipo type si el game object tiene a alguno adjuntado, o null si no lo tiene.

GetComponent es la principal forma de acceder a otros componentes. En javascript el tipo de un script es siempre el nombre del script que será visto en la vista project. Con esta función puedes acceder a componentes pre-construidos o a scripts.

using UnityEngine;

public class GetComponentExample : MonoBehaviour { void Start( ) { HingeJoint hinge = gameObject.GetComponent( typeof(HingeJoint) ) as HingeJoint;

if( hinge != null ) hinge.useSpring = false; } }

public T GetComponent ();

Descripción

Versión genérica. Para más detalles, mira la página de Funciones genéricas.

using UnityEngine;

public class GetComponentExample : MonoBehaviour { void Start( ) { HingeJoint hinge = gameObject.GetComponentInParent<HingeJoint>( );

if( hinge != null ) hinge.useSpring = false; } }

public Component GetComponent (string type);

Parámetros

type El tipo de Component para recuperar.

Descripción

Devuelve el componente que sea de tipo type si el game object tiene a alguno adjuntado, o null si no lo tiene.

It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type.

using UnityEngine;

public class GetComponentExample : MonoBehaviour { void Start( ) { HingeJoint hinge = gameObject.GetComponentInParent( "HingeJoint" ) as HingeJoint;

if( hinge != null ) hinge.useSpring = false; } }