Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by book · Mar 22, 2013 at 05:06 PM · instantiatetransformnullreferenceexceptionfindwithtag

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Mar 23, 2013 at 07:15 AM 1
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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?)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image book · Mar 23, 2013 at 10:37 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges