Instantiating Cameras on startup

Hi everyone,

I am rather new to unity and recently finished the Tanks! tutorial. Since I used to love a game ages ago, I wanted to write a copycat of that using the Tanks! game as base from where to proceed.

One of the tasks I need to do is to use a split screen. I didn’t want to attach the cameras directly to the tanks, as I wanted a view that is independent of the tanks rotation (i.e. i want the tanks to rotate, not the background).

If anyone knows how to attach the cameras in a way that this would be achieved, I could do that and the rest of the post isn’t important anymore!

So, I opted to instantiate Cameras whenever I instantiate a tank and then use a script to attach camera to tank . Unfortunately I did something wrong and get the following error:
NullReferenceException: Object reference not set to an instance of an object
GameManager.SetupCamera () (at Assets/Scripts/Managers/GameManager.cs:261)
GameManager.Start () (at Assets/Scripts/Managers/GameManager.cs:46)
but all objects exist, as the debugger shows:
Parameters for instantiate are CameraInstance: SplitScreenCamera (UnityEngine.Camera) Tanks spawnpoint: (-3.0, 0.0, 30.0) Cameraadjustment: (-18.0, 20.0, -10.0) CameraRotation: (40.0, 60.0, 0.0, 0.0)
UnityEngine.Debug:Log(Object)
GameManager:SetupCamera() (at Assets/Scripts/Managers/GameManager.cs:260)
GameManager:Start() (at Assets/Scripts/Managers/GameManager.cs:46)
So I have no clue what the error means.
Here is the code :
```csharp
* private void SetupCamera()
{

    Rect[] m_Rectangles = new Rect[m_Tanks.Length];
    for (int i = 0; i < m_Rectangles.Length; i++)
    {

        m_Rectangles[i].Set(i / m_Rectangles.Length, 0, (i+1)/ m_Rectangles.Length, 1);

    }


    SplitScreen[] m_Cameras = new SplitScreen[m_Tanks.Length];
    for (int i = 0; i < m_Cameras.Length; i++)
    {
        if (!m_CameraInstance) Debug.Log("CameraInstance not set!!");
        else Debug.Log("CameraInstance set to " + m_CameraInstance);
        Debug.Log("Parameters for instantiate are CameraInstance: " + m_CameraInstance + " Tanks spawnpoint: " + m_Tanks[i].m_SpawnPoint.position + " Cameraadjustment: " + m_CameraPositionRelativeToTank + " CameraRotation: " + m_CameraRotation);
        m_Cameras[i].m_Instance =
            Instantiate(m_CameraInstance,m_Tanks[i].m_SpawnPoint.position + m_CameraPositionRelativeToTank, m_CameraRotation) as Camera;
        m_Cameras[i].SetCameraRect(m_Rectangles[i]);
        m_Cameras[i].m_CameraTarget = m_Tanks[i].m_Instance.transform;
        Debug.Log("Instatiated Camera Instance " + i + " with Rectangles " + m_Rectangles[i] + " at position " + m_Tanks[i].m_SpawnPoint.position + m_CameraPositionRelativeToTank
            + " with rotation " + m_CameraRotation + " and target " + m_Cameras[i].m_CameraTarget);


    }



}*</em>

```
the lines in question are 19 and 20.
Thanks a lot!

Hi there,

According to the error the type of your m_CameraInstance var is SplitScreenCamera so you cannot cast it to Camera which you’re doing at the end of line 19 (“as Camera”).

What you can do is cast it to SplitScreenCamera doing “as SplitScreenCamera” or fetch the camera reference from the object if it has one, with m_CameraInstance.GetComponent() in which case your code should run.

As for following your tank, unity has a FollowTarget component in the standard assets that you could use.

Thanks for the answer. Casting as SplitScreenCamera does not work, unfortunately. This gives the error:

Assets/Scripts/Managers/GameManager.cs(262,135): error CS0246: The type or namespace name `SplitScreenCamera’ could not be found. Are you missing a using directive or an assembly reference?

Okay, I changed it now to create an instance of SplitScreen and then set the Camera there to the prefab. It works now, but I have the strange occurrence that the right splitscreen (for player 2) is on the left and the left one on the right. But that isn’t that big of an issue.

Thanks.

private void SetupCamera()
    {

        Debug.Log("length of Tanks array = " + m_Tanks.Length);

        Rect[] m_Rectangles = new Rect[m_Tanks.Length];
        for (int i = m_Rectangles.Length - 1 ; i  >  -1; i--)
        {
            float j = i;
            float xpos = j /  m_Rectangles.Length;
            float width = 1f / m_Rectangles.Length ;
            Debug.Log("xpos and width = " + xpos + width);
            m_Rectangles[i].Set(xpos, 0, width, 1);
            Debug.Log("m_Rectangles[] = " + m_Rectangles[0] + m_Rectangles[1]);

        }
       

        SplitScreen[] m_Cameras = new SplitScreen[m_Tanks.Length];
        for (int i = 0; i < m_Cameras.Length; i++)
        {
            if (!m_SplitScreen) Debug.Log("CameraInstance not set!!");
            else Debug.Log("CameraInstance set to " + m_CameraInstance);
            Debug.Log("Parameters for instantiate are CameraInstance: " + m_CameraInstance + " Tanks spawnpoint: " + m_Tanks[i].m_SpawnPoint.position + " Cameraadjustment: " + m_CameraPositionRelativeToTank + " CameraRotation: " + m_CameraRotation);
            m_Cameras[i] =
                Instantiate(m_SplitScreen, m_Tanks[i].m_SpawnPoint.position + m_CameraPositionRelativeToTank, m_CameraRotation) as SplitScreen;
            Debug.Log("1");
            m_Cameras[i].m_Instance = m_CameraInstance;
            m_Cameras[i].SetCameraRect(m_Rectangles[i]);
            m_Cameras[i].m_CameraTarget = m_Tanks[i].m_Instance.transform;
          


        }



    }