Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ashton-Benedict · Jul 20, 2016 at 07:19 PM · errorvariablenull reference exception

NullReference. Need Help.

I am using a variable from another script (facingRight). I get the error NullReferenceException, and I cant figure out what exactly I need to reference.

 public class BulletTrail : MonoBehaviour {
 
     public int moveSpeed = 0;
 
     private PlayerController playerController;
 
     void Awake()
     {
         playerController = transform.root.GetComponent<PlayerController>();
     }
 
     void Update()
     {
             if (!playerController.facingRight)
             {
                 transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
                 Destroy(gameObject, 1);
             }
             if (playerController.facingRight)
             {
                 transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
                 Destroy(gameObject, 1);
             }
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by Jessespike · Jul 20, 2016 at 07:31 PM

I'm guessing playerController is failing to get the component, which throws the error when you try to use it.

  void Awake()
  {
      playerController = transform.root.GetComponent<PlayerController>();
  }

^ This, are you sure this is working? Verify with a Debug.Log:

 Debug.Log("is playerController null? " + (playerController == null));

Make adjustments so playerController is not null.

Comment
Add comment · Show 6 · 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 Ashton-Benedict · Jul 20, 2016 at 08:35 PM 0
Share

I put that in and it says its true and then spams me with the error.

avatar image Jessespike Ashton-Benedict · Jul 20, 2016 at 09:40 PM 0
Share

yes, the playerController is null. It's not on the transform's root. Find a new way to store the reference. Either make playerController into a public and delete the Awake function OR update the Awake code so it points to the transform with the component. I don't know how you organized the objects, so I can't say what the code will look like. $$anonymous$$aybe:

 playerController = transform.GetComponent<PlayerController>();

or maybe even:

 playerController = FindObjectOfType<PlayerController>().transform;
avatar image Ashton-Benedict Jessespike · Jul 20, 2016 at 09:56 PM 0
Share

I don't understand, and I tried it, it didn't work :P what do you mean not on the transform's root?

Show more comments
avatar image Jessespike · Jul 21, 2016 at 05:18 AM 0
Share

PlayerController component needs to be on a GameObject so it can be found

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

74 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Javascript error 1 Answer

List suddenly auto-clear after WebRequest (in another Script)? 1 Answer

Unity3d Script does not work anymore 1 Answer

C# - Cannnot access variable in another script unless I get the component everytime. 1 Answer

Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 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