GetComponent









How do I use Get Component
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: JumpAndRunMovement.cs Copy
15     void Awake()
16     {
17         m_Animator = GetComponent();
18         m_Body = GetComponent();
19         m_PhotonView = GetComponent();
20     }
File name: OnAwakePhysicsSettings.cs Copy
10     public void Awake()
11     {
12         if (!photonView.isMine)
13         {
14             Rigidbody attachedRigidbody = GetComponent();
15             if (attachedRigidbody != null)
16             {
17                 attachedRigidbody.isKinematic = true;
18             }
19             else
20             {
21                 Rigidbody2D attachedRigidbody2d = GetComponent();
22                 if (attachedRigidbody2d != null)
23                 {
24                     attachedRigidbody2d.isKinematic = true;
25                 }
26             }
27         }
28     }
File name: ClickAndDrag.cs Copy
11  void Update ()
12  {
13         if (!photonView.isMine)
14         {
15             return;
16         }
17
18      InputToEvent input = Camera.main.GetComponent();
19      if (input == null) return;
20         if (!following)
21         {
22             if (input.Dragging)
23             {
24                 camOnPress = this.transform.position;
25                 following = true;
26             }
27             else
28             {
29                 return;
30             }
31         }
32         else
33         {
34             if (input.Dragging)
35             {
36                 Vector3 target = camOnPress - (new Vector3(input.DragVector.x, 0, input.DragVector.y) * factor);
37                 this.transform.position = Vector3.Lerp(this.transform.position, target, Time.deltaTime*.5f);
38             }
39             else
40             {
41                 camOnPress = Vector3.zero;
42                 following = false;
43             }
44         }
45  }
File name: MaterialPerOwner.cs Copy
12     void Start()
13     {
14         m_Renderer = GetComponent();
15     }
File name: OnClickRequestOwnership.cs Copy
28     public void ColorRpc( Vector3 col )
29     {
30         Color color = new Color( col.x, col.y, col.z );
31         this.gameObject.GetComponent().material.color = color;
32     }
File name: NamePickGui.cs Copy
15     public void Awake()
16     {
17         this.guiCenteredRect = new Rect(Screen.width/2-GuiSize.x/2, Screen.height/2-GuiSize.y/4, GuiSize.x, GuiSize.y);
18         this.chatComponent = this.GetComponent();
19
20         if (this.chatComponent != null && chatComponent.enabled)
21         {
22             Debug.LogWarning("When using the NamePickGui, ChatGui should be disabled initially.");
23
24             if (this.chatComponent.chatClient != null)
25             {
26                 this.chatComponent.chatClient.Disconnect();
27             }
28             this.chatComponent.enabled = false;
29         }
30
31         string prefsName = PlayerPrefs.GetString(NamePickGui.UserNamePlayerPref);
32         if (!string.IsNullOrEmpty(prefsName))
33         {
34             this.InputLine = prefsName;
35         }
36     }
File name: BallControl.cs Copy
20  void Start () {
21   rb2d = GetComponent ();
22   Invoke ("GoBall", 2);
23  }
File name: PlayerControls.cs Copy
14  void Start () {
15   rb2d = GetComponent ();
16  }
File name: MoveCam.cs Copy
13  void Start ()
14     {
15         camTransform = GetComponent().transform;
16      originalPos = camTransform.position;
17
18         randomPos = originalPos + new Vector3(Random.Range(-2, 2), Random.Range(-2, 2), Random.Range(-1, 1));
19     }
File name: DemoMecanimGUI.cs Copy
42     public void FindRemoteAnimator()
43     {
44         if( m_RemoteAnimator != null )
45         {
46             return;
47         }
48
49         // the prefab has to be tagged as Player
50         GameObject[] gos = GameObject.FindGameObjectsWithTag( "Player" );
51         for( int i = 0; i < gos.Length; ++i )
52         {
53             PhotonView view = gos[ i ].GetComponent();
54             if( view != null && view.isMine == false )
55             {
56                 m_RemoteAnimator = gos[ i ].GetComponent();
57             }
58         }
59     }

GetComponent 161 lượt xem

Gõ tìm kiếm nhanh...