I have two empty game objects (BlueTeam and RedTeam). The BlueTeam needs to set its opponent as the RedTeam and vice versa. When a team is instantiated it as to generate its players and place them on there home regions which are created when I Instantiate the pitch. When I instantiate one team only my players appear at the correct positions with no error. When I try to instantiate both teams at the same time I have a “Null Exception The Object which you are trying to reference doe not exist”. I know what the error means but I cant seem to get why that is happening.
Here is some code of what I am trying to do.
Here is my start function for my SoccerPitch
// Use this for initialization
void Start()
{
//create the soccer ball
m_pBall = Instantiate(m_pBall) as SoccerBall;
//create the teams
m_pRedTeam = Instantiate(m_pRedTeam) as SoccerTeam;
m_pBlueTeam = Instantiate(m_pBlueTeam) as SoccerTeam;
m_bGameOn = true;
m_bGoalKeeperHasBall = false;
m_bPaused = false;
}
Here is my Start function for my SoccerTeam
// Use this for initialization
void Start () {
//initialize soccerball
m_pPitch = GameObject.FindGameObjectWithTag("soccerpitch").GetComponent<SoccerPitch>();
//initilize team home regions
if(transform.tag == "redteam")
{
m_pHomeGoal = GameObject.FindGameObjectWithTag("redgoal").GetComponent<Goal>();
m_pHomeGoal.m_vFacing = new Vector3(0,1,0);
GeneratePlayers(m_pPlayersHomeRegions);
m_pOpponents = GameObject.FindGameObjectWithTag("blueteam").GetComponent<SoccerTeam>();
m_POpponentsGoal = GameObject.FindGameObjectWithTag("bluegoal").GetComponent<Goal>();
}
if(transform.tag == "blueteam")
{
m_pHomeGoal = GameObject.FindGameObjectWithTag("bluegoal").GetComponent<Goal>();
m_pHomeGoal.m_vFacing = new Vector3(0,-1,0);
m_pOpponents = GameObject.FindGameObjectWithTag("redteam").GetComponent<SoccerTeam>()
m_POpponentsGoal = GameObject.FindGameObjectWithTag("redgoal").GetComponent<Goal>();
//create players for this team
GeneratePlayers(m_pPlayersHomeRegionsblue);
}
m_pReceivingPlayer = null;
}
Here is my start unction for my Field player.
// Use this for initialization
public void Start(){
m_vVelocity = new Vector3(0,0,0);
m_vHeading = new Vector3(0,0, 0);
rigidbody.velocity = new Vector3(0,0, 0);
//m_pBall = GameObject.FindGameObjectWithTag("soccerBall");
m_pSteering = new SteeringBehaviours(this.gameObject.GetComponent<FieldPlayer>(), GameObject.FindGameObjectWithTag("soccerBall").GetComponent<SoccerBall>());
//initilize the statemachine
m_pStateMachine = new StateMachine<FieldPlayer>(this);
//intilize the states
m_pStateMachine.SetCurrentState(Wait.Instance());
this.Steering().SetTarget(this.HomeRegion().Pos());
//set the players start position
transform.position = (this.HomeRegion()).Pos() ;
}
The Null Exception error points me to the FieldPlayer’s start function where I try to set the position of the FieldPlayer(transform.position = (this.HomeRegion()).Pos()