Object reference not set to an instance of an object (Tanks Tutorial)?
I'm new to Unity and I'm following the Tanks! Tutorial. I've followed the tutorial exactly but keep getting the following error message: "NullReferenceException: Object reference not set to an stance of an object; CameraControl.FindAverage Position () (at Assets/Scripts/Camera/CameraControl.cs:52) It seems to be referring to this block of code:
private void FindAveragePosition ()
{
Vector3 averagePos = new Vector3 ();
int numTargets = 0;
for (int i = 0; i < m_Targets.Length; i++)
{
if (!m_Targets[i].gameObject.activeSelf)
continue;
averagePos += m_Targets[i].position;
numTargets++;
}
Line 52 is the "if (!m_Targets[i].game.Object.activeSelf) one.
I'm not exactly sure (I only started Unity a few days ago) I think it's this line:
public Transform[] m_Targets;
No, that line just creates an array of Transforms named m_Targets. Something, somewhere else (either a portion of this script that you didn't post, or another script, or manually within the Unity Inspector) should be inserting one or more Transforms into the m_Targets variable.
I actually fixed that error now, but it still has the same error message except now it's about a different script (the Tank $$anonymous$$anager one) about line 33, which is this line of code:
m_$$anonymous$$ovement.m_PlayerNumber = m_PlayerNumber;
So, in that case, I'd guess that "m_$$anonymous$$ovement" is null. Look around and try to see where it gets a value assigned to it.
Without more info, it's difficult to say more.
Answer by dos360 · Mar 16, 2016 at 12:38 PM
I have the same problem as caroline13.
The m_Targets array is loaded with a TankManager array called m_Tanks in the SpawnAllTanks() method in GameManager.cs ; and m_Tanks is initialised in the IDE by assigning a colour and dropping a pair of spawnpoints to an array to two tanks, one blue, the other red.
if two people are making the same mistake, it is likely that there is a problem with the source code supllied. please help.