- Home /
Why am I getting a nullReferenceException?
I'm making a racing game with checkpoints. I'm instantiating a player in a script called manager and also setting the tag name of the instantiated player object to "Player". But also the prefabbed object is already tagged Player
Manager Script :
Instantiate(player1Car, p1Pos.position, p1Pos.rotation);
can now move my instantiated player around the track. But I'm getting a null reference error in my checkpoint script when my character crosses a checkpoint in my game. But my code in the checkpoint script is FindWithTag("Player") and I set the tag of the instantiated player object to "Player" when I created it. So why can't it find player? why can't it find the tag?*
CheckPoint Script :
void Start()
{
//find the instantiated player and reference it with PlayerTransform
PlayerTransform = GameObject.FindWithTag("Player").transform;
}
//when the player car goes through the checkpoint this trigger is called
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
// the next line of code is where the error appears.
int cCheckPoint = PlayerTransform.GetComponent<PlayerCheckPoints>().currentCheckPoint;
int cLap = PlayerTransform.GetComponent<PlayerCheckPoints>().currentLap;
the error that appears on that line (int cCheckPoint etc) is
NullReferenceException: Object reference not set to an instance of an object CheckPoint.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Checkpoints/CheckPoint.cs:56)
Help really appreciated. This has been bugging me all day. I hope I explained it right and it's not too confusing.
Try:
Put a Debug.Log() statement just before the error line that outputs PlayerTransform to check if it is null.
Verify the player is tagged "Player"
Verify there is only one object that is tagged "Player."
Verify that the only object marked "Player" has a "PlayerCheckPoints" script attached.
Answer by sparkzbarca · Mar 23, 2013 at 09:06 AM
yea it could be playertransform
or it could be playercheckpoints component isnt attached to the prefab
or it could be the currentCheckpoint in that script is set to null
copy paste this
Debug.Log(PlayerTransform.gameobject.name); Debug.Log(PlayerTransform.gameobject.GetComponent()); Debug.Log(PlayerTransform.gameobject.GetComponent().currentCheckPoint);
if you have
null null null
PLayerTransform doesnt exist
something null null
the component isn't attached
something something null
since it's letting you type currentCheckPoint the varaible exists so the issue is its set to null
there is no current check point (perhaps because you jsut cleared one there isn't a current one?)
Thanks guys. I just entered your code sparkzbarca and I figured out it was the currentcheckpoint variable that was returning null.
This was because that script was on a child object of the Player1 object.
So I should have put GetComponentInChildren ins$$anonymous$$d of GetComponent.
int cCheckPoint = PlayerTransform.GetComponentInChildren().currentCheckPoint; It works now. Thanks!
Your answer
Follow this Question
Related Questions
How can I get the position of transforms inside an instantiated game object? 1 Answer
Why does this Instantiate give a Null Reference Exception? 1 Answer
Endless 3D plane repetition animated by script is not moving 0 Answers
Rotating a certain axis offsets the other ones? 1 Answer
Getting instance of an sub object rather than the original's subobject 0 Answers