Search Unity

Changing cameras with static methods isn't working

Discussion in 'Scripting' started by matt123miller, Oct 24, 2014.

  1. matt123miller

    matt123miller

    Joined:
    Oct 24, 2014
    Posts:
    5
    Hi all,
    I'm trying to switch from third to first person when the player enters a collider and it won't work.
    I have my very simple class on the collider object, calling a method.
    Code (CSharp):
    1. public class ventCrouchToggle : MonoBehaviour {
    2.  
    3.    void OnTriggerEnter(Collider other)
    4.     {
    5.         CamCallToggle.SetToggleVar();
    6.     }
    7.  
    8.     }
    Then this code should change the cameras whenever I call SetToggleVar() but it won't.
    Code (CSharp):
    1. public class CamCallToggle : MonoBehaviour
    2. {
    3.  
    4.     public static GameObject camFirstPerson; // first person camera
    5.     public static GameObject camThirdPerson; // third person camera
    6.     public static bool check = true;         // New check-variable
    7.  
    8.     void OnAwake()
    9.     {
    10.         camFirstPerson = GameObject.FindWithTag("CamFirstPerson");
    11.         camThirdPerson = GameObject.FindWithTag("MainCamera");
    12.     }
    13.  
    14.  
    15.     public static void SetToggleVar()
    16.     {
    17.         Debug.Log("check");
    18.  
    19.         check = !check;
    20.         ToggleCall();
    21.     }
    22.  
    23.     public static void ToggleCall()
    24.     {
    25.  
    26.        if (check)
    27.         {
    28.           camFirstPerson.SetActive(false);
    29.           camThirdPerson.SetActive(true);
    30.         }
    31.         else
    32.         {
    33.           camFirstPerson.SetActive(true);
    34.           camThirdPerson.SetActive(false);
    35.         }
    36.     }
    37. }
    The really annoying thing is that I had this code working replacing the collider with a button press to test the code, but the collider is required to implement my mechanics. Any help?
     
  2. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Does "OnTriggerEnter" actually trigger? The rest of the code seems fine

    Quick checklist:
    - is the script on the player and active?
    - does the player or the object have a rigidbody?
    - is the collider's "is trigger" checked?
     
  3. matt123miller

    matt123miller

    Joined:
    Oct 24, 2014
    Posts:
    5
    Mostly yes
    Yes
    Yes

    I say mostly yes just to be specific as the longer, CamCallToggle script is on my player controller object, which in it's hierarchy has my 2 cameras to enable and disable. The ventCrouchToggle is on the entrance to a vent for sneaking through.

    Also, my public class variables don't show in the inspector, very wierd.
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Make them public instead of public static.
    Static makes them public but not show in the inspector.
     
  5. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    If ventCrouchToggle is on the entrance to the vent, then the player's collider has to be marked as trigger. But then he can go through stuff. You might want to put that script on the player instead.

    Also, as Nubz said, public static variables don't show in the inspector.