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!