Search Unity

How To Get Other GameObject's Component Without Manually Drag It And Assign It?

Discussion in 'Scripting' started by joeyxiong1994, Jul 30, 2019.

  1. joeyxiong1994

    joeyxiong1994

    Joined:
    Apr 18, 2017
    Posts:
    20
    Hi, in my game, I have a player object which has a PlayerControl script component attached with, and I have a camera with a CameraControl script component attached. I want use CameraControl inside the PlayerControl, so far I can only declare

    Code (csharp):
    1.  
    2. public CameraControl camControl;
    3.  
    And then I assign it through inspector. I just wonder if I can directly get it in my Start() function. such like below:
    Code (csharp):
    1.  
    2. void Start () {
    3. camControl = *******.GetComponent<CameraControl>();
    4. }
    5.  
    But I don't know how to define ******* to reach the Camera object as camera object is not a child object of player object