Search Unity

Can't access script on current Camera

Discussion in 'Scripting' started by II_Spoon_II, Oct 18, 2018.

  1. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    Hello everyone ^^

    So I am in the process of making a multiplayer game with photon and unity, in order to make each players face a different view, I made 2 camera in the scene, and for each player, a camera gets enabled (it's only 1v1 ) and it works perfectly fine, here is the script I did to do so
    Code (CSharp):
    1. if (!PhotonNetwork.connected)
    2.             {
    3.                 SceneManager.LoadScene("RTSMainScene");
    4.                 return;
    5.             }
    6.        
    7.             else
    8.             {
    9.  
    10.                 if (PhotonNetwork.isMasterClient)
    11.                 {
    12.                     PhotonNetwork.Instantiate(Player1.name, Vector3.zero, Quaternion.identity, 0);
    13.                     cam1.enabled = true; cam2.enabled = false;
    14.                 }
    15.                 else
    16.                 {
    17.                     PhotonNetwork.Instantiate(Player2.name, Vector3.zero, Quaternion.identity, 0);
    18.                     cam1.enabled = false; cam2.enabled = true;
    19.                 }
    I gave each camera a different tag, "blue" and "red" depending on the view, but when trying to get an attached script to it,, I get an error (null reference set to the instance of an object) even tho the camera does exist and the script too
    Code (CSharp):
    1. Camera cam;
    2.  
    3.         // Use this for initialization
    4.         void Start()
    5.         {
    6.              cam = Camera.current;
    7.         }
    8.         void OnMouseDown()
    9.         {
    10.  
    11.  
    12.              
    13.             MyPlayerManager GM = cam.GetComponent<MyPlayerManager>();
    14.  
    15.        
    16.             if (GM.isTarget == false)
    17.             {
    18.                 if (cam.tag  == gameObject.tag) {
    19.  
    20.                 GM.BaseTower = gameObject;
    21.                 GM.isTarget = true;
    22.                 }
    23.             }
    I don't know if the camera.current is the problem but seems to me like it is what I need to call the current active camera. it gives an error at line 13 (while running) which is
    MyPlayerManager GM = cam.GetComponent<MyPlayerManager>();


    Thanks for your time ^^
     
    Last edited: Oct 18, 2018
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you get a null reference exception on that line, that means that "cam" is null.

    One of the following must be true:
    1) Your Start() function never executed
    2) Camera.current was null at the time it executed
    3) The camera was destroyed sometime between then and now
    4) Something else changed the value of the "cam" variable between then and now
     
  3. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    Hello^^ thanks for your reply, so to check these possibilities, I made the first script run in an awake function to be executed before the start fucntion, still the same issue
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    The sorts of things that might hypothetically prevent Start() from running would probably prevent Awake(), too. This can happen if you try to use a script on an inactive GameObject, or if you try to call functions on a script during the same frame that you instantiate it.

    I don't think it's very likely in this case, but if you wanted to test it, a better option would be to add a Debug.Log() statement to your Start() function, so you can see that it's running (or not). While you're at it, you should also print out whether cam is null at that time.

    I think the most likely problem is that you are not getting the Camera that you actually want. Camera.current says it should only be used for low-level render control. And since you specifically have two different cameras in your scene for a special purpose unique to your game, it's unlikely that any standard Unity call is going to know which one you are interested in.

    Have you considered using something that would look for a Camera in a position relative to this script's place in the object hierarchy? GetComponent<>() could find a camera attached to the same object, while GetComponentInParent<>() or GetComponentInChildren<>() could look for one above/below your position.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I havent red though all the posts, but are you sure you calling the correct camera?

    You may say cam.blah thinking you tell the red camera to do blah while you'rr actually telling it to the blue one while its off
     
  6. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    I will try those now, actually I made public cameras in the script and assigned them, but let me check your suggestions ^^ thanks btw for your answer, and sorry for replying late had school
     
  7. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    that's why I used cam=Camera.current
     
  8. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    Actually it didn't pass the test, seems like the problem is with the start function
     
  9. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    So the first line of start dosen't send anything, while the thirs one, gives ("null reference...")
    void Start()
    {
    Debug.Log("working");
    cam = Camera.current;
    Debug.Log("the camera working is" +cam.name);
    }
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
  11. II_Spoon_II

    II_Spoon_II

    Joined:
    Jun 16, 2018
    Posts:
    180
    Hey, I solved my problem, it was pretty dumb of me, it's cause the class was initiated in a previous scene so the start function was called in the lobby and there was no cameras yet x) thanks btw