Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetComponent script woes

Discussion in 'Scripting' started by super_tnt, Sep 2, 2014.

  1. super_tnt

    super_tnt

    Joined:
    May 17, 2013
    Posts:
    2
    So I'm working on a script that allows the player to enter a vehicle. To do this I have camera called carcam which uses the smoothfollow script and what i need to do is activate this camera and script and set the target to the car which has been found earlier, unfortunately I'm hitting a bunch of errors that I don't understand

    Code (CSharp):
    1.     void Update () {
    2.         if (Input.GetKeyDown (KeyCode.E)) {
    3.             Collider[] collider = Physics.OverlapSphere(transform.position,10);
    4.             foreach( Collider c in collider)
    5.             {
    6.                 print(c.name);
    7.                 if (c.tag == "car")
    8.                 {
    9.                     c.enabled = true;
    10.                     GetComponent<MouseLook>().enabled = false;
    11.                     GetComponent<CharacterController>().enabled = false;
    12.                     GetComponent<Camera>().enabled = false;
    13.                     GameObject g = GameObject.Find("carcam");
    14.                     g.GetComponent("SmoothFollow").enabled = true;
    15.                 }
    16.             }
    17.         }
    18.     }
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Don't use quotes in GetComponent; also you have to cast to the correct type or use generics like you did with the earlier GetComponents.

    --Eric
     
  3. super_tnt

    super_tnt

    Joined:
    May 17, 2013
    Posts:
    2
    Cheers, that worked perfectly now I got to figure the rest out, thanks