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 /
This question was closed Jun 12, 2017 at 07:26 AM by Hellium for the following reason:

http://answers.unity3d.com/questions/47830/what-is-a-null-reference-exception-in-unity.html

avatar image
-1
Question by Xaos95 · Jun 12, 2017 at 06:52 AM · c#cameranullreferenceexception

Im Getting an error saying: Object reference not set to an instance of an object although nothing is null.

So.. i am following this tutorial on youtube and i am getting stuck at making the camera. I have tried to add the camera script before the player script in the Script execution order but i still get the same error on line 20. When i press play the script is disabled in the inspector and when i enable it i get another error in line 42 which is added repeatedly.

Here is the camera script:

 public class ThirdPersonCamera : MonoBehaviour {
 
 
     [SerializeField] Vector3 cameraOffset;
     [SerializeField] float damping;
 
     Transform cameraLookTarget;
 
 
     Player localPlayer;
 
 
     void Awake () 
     {
         GameManager.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;;
         cameraLookTarget = localPlayer.transform.Find ("cameraLookTarget");
 
         if (cameraLookTarget == null)
             cameraLookTarget = localPlayer.transform;
 
 
 
     }
 
 
 
     void HandleOnLocalPlayerJoined(Player player)
     {
         localPlayer = player;
     }
 
 
 
     void Update () 
     {

         Vector3 targetPosition = cameraLookTarget.position + localPlayer.transform.forward * cameraOffset.z +
                 localPlayer.transform.up * cameraOffset.y +
                 localPlayer.transform.right * cameraOffset.x;
 
 
         transform.position = Vector3.Lerp (transform.position, targetPosition, damping * Time.deltaTime);
     }
 
 }






Here is the Player script:

 [RequireComponent(typeof(CharacterMovement))]
 public class Player : MonoBehaviour {
 
 
     [System.Serializable]
     public class MouseInput{
         public Vector2 Damping;
         public Vector2 Sensitivity;
     }
 
 
     [SerializeField] float speed;
     [SerializeField] MouseInput MouseControl;
 
 
     private CharacterMovement m_characterMovement;
     public CharacterMovement characterMovement
     {
         get{
             if (m_characterMovement == null) 
             {
                 m_characterMovement = GetComponent<CharacterMovement> ();
             }
             return m_characterMovement;
         }
     }
 
     InputController playerInput;
 
 
     void Awake () 
     {
         playerInput = GameManager.Instance.inputController;
         GameManager.Instance.LocalPlayer = this;
     }
     
 
     void Update () 
     {
         Vector2 direction = new Vector2 (playerInput.Vertical * speed, playerInput.Horizontal * speed);
         characterMovement.Move (direction);
     }
 }



I am positive that the errors has to do with the "cameralooktarget" but i would like to know why and how i could fix it. Tell me if you need anymore details, any kind of help is appreciated.

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 ShadyProductions · Jun 12, 2017 at 06:52 AM 0
Share

$$anonymous$$aybe localPlayer is null?

1 Reply

  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 12, 2017 at 07:21 AM

Your main problem is that you set your localPlayer variable inside the "HandleOnLocalPlayerJoined" callback. However you just subscribed the callback inside Awake and right after you try to access the localPlayer variable which of course is not set yet. You should move all the remaining code inside Awake into "HandleOnLocalPlayerJoined".

Another big problem may be your "GameManager" singleton. How is "Instance" implemented? Does it lazy load? If it's a MonoBehaviour and it does not lazy load you don't want to access it inside Awake of other classes since the initialization order of the classes matters.

You can use the Script Execution Order to avoid such problems, but when the project gets bigger this will quickly get out of control.

Am I right that "OnLocalPlayerJoined" is invoked from inside the setter of "GameManager.LocalPlayer"? That would require a strict order of:

  • GameManager

  • ThirdPersonCamera

  • Player

If "Player" would initialize before "ThirdPersonCamera", you would have already assigned the LocalPlayer in the GameManager before ThirdPersonCamera has subscribed to the event.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

[Solved]NullReferenceException problem 2 Answers

Distribute terrain in zones 3 Answers

NullRefenceException error 1 Answer

NullReferenceException .. problem with Camera.main.transform 2 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