- Home /
Null Reference Exception ? :/
UnityEngine.GameObject.GetComponent<Player1> () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:27)
xa.Start () (at Assets/Scripts/xa.cs:30)
I get this error when i go to run my game
Here is my code
using UnityEngine;
using System.Collections;
public class xa : MonoBehaviour
{
public static Ball ball;
public static AudioManager audioManager;
public static ScoreManager scoreManager;
public static Player1 player1;
public static Player2 player2;
//public static Player3 player3;
//public static Player4 player4;
public static bool gameOver = false;
// set to false if game is configured for CTF
public static bool letsPlayKeepaway = false;
// layers
public const int Team1Goal = 9;
public const int Team2Goal = 10;
void Start()
{
// cache these so they can be accessed by other scripts
ball = GameObject.FindWithTag("Ball").GetComponent<Ball>();
scoreManager = gameObject.GetComponent<ScoreManager>();
audioManager = gameObject.GetComponent<AudioManager>();
player1 = GameObject.Find("Player1").GetComponent<Player1>();
player2 = GameObject.Find("Player2").GetComponent<Player2>();
//player3 = GameObject.Find("Player3").GetComponent<Player3>();
//player4 = GameObject.Find("Player4").GetComponent<Player4>();
}
}
I dont know what i need to change or set ? If anyone has some advice?
I start with an empty scene and when i join the server it creates a player, but when it goes to start the match again the error comes up? any ideas?
I have never work on a game/game-prototype that uses network/server, so I have no experience on that.
The only thing I am sure of is that Player1
does not exist (yet) when the GameObject.Find()
is called in the Start()
of the xa.cs
.
Do you have an example? Trying multiple ways but cant seem to write it correctly?
Answer by Chronos-L · May 04, 2013 at 09:07 AM
"Player1" doesn't exists in your scene, so GameObject.Find("Player1")
returns a null
and the null
created the null-ref exception when you call GetComponent< Player1 > ()
.
Side Note: Please format your code properly for your own convenience. I did it for you this once.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
Player instantiates Backwords 0 Answers
Importing 2d graphics - adopting scene darkness - easy answer 0 Answers