Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dragonking300 · Apr 03, 2017 at 05:09 PM · scripting problemscripting beginnercamera-movementc# tutorial

Object not set to a instance of a object

I have no clue what I am doing wrong.

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour
 {
     [SerializeField]
     private Transform target = null;[SerializeField]
     private float distance = 3.0f;[SerializeField]
     private float height = 1.0f;[SerializeField]
     private float damping = 5.0f;[SerializeField]
     private bool smoothRotation = true;[SerializeField]
     private float rotationDamping = 10.0f;
 
     [SerializeField]
     private Vector3 targetLookAtOffset; // allows offsetting of camera lookAt, very useful for low bumper heights
     [SerializeField]
     private float bumperDistanceCheck = 5f; // length of bumper ray
     [SerializeField]
     private float bumperCameraHeight = 1.0f; // adjust camera height while bumping
     [SerializeField]
     private Vector3 bumperRayOffset; // allows offset of the bumper ray from target origin
     private Transform Player;                        /// <Summary>
                                                      /// If the target moves, the camera should child the target to allow for smoother movement. DR
                                                      /// </Summary>
     private void Awake()
     {
         GetComponent<Camera>().transform.parent = Player;
     }
     private void FixedUpdate()
     {
         Vector3 wantedPosition = Player.TransformPoint(0, height, -distance);
         // check to see if there is anything behind the target
         RaycastHit hit;
         Vector3 back = Player.transform.TransformDirection(-1 * Vector3.forward);
         // cast the bumper ray out from rear and check to see if there is anything behind
         if (Physics.Raycast(Player.TransformPoint(bumperRayOffset), back, out hit, bumperDistanceCheck)
             && hit.transform != target) // ignore ray-casts that hit the user. DR
         {
             // clamp wanted position to hit position
             wantedPosition.x = hit.point.x;
             wantedPosition.z = hit.point.z;
             wantedPosition.y = Mathf.Lerp(hit.point.y + bumperCameraHeight, wantedPosition.y, Time.deltaTime * damping);
         }
         transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * damping);
         Vector3 lookPosition = Player.TransformPoint(targetLookAtOffset);
         if (smoothRotation)
         {
             Quaternion wantedRotation = Quaternion.LookRotation(lookPosition - transform.position, Player.up);
             transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
         }
         else
             transform.rotation = Quaternion.LookRotation(lookPosition - Player.position, target.up);
     }
 }
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
1

Answer by Commoble · Apr 03, 2017 at 05:15 PM

A null reference exception happens when you try to call someObject.someMember when someObject is null. Your error message will tell you which line this happened in. Look at that line, figure out which null variable you're trying to access, and decide whether A) it really should never be null there and there's a problem that needs to be fixed, or B) you can do

 if (someObject == null)
 {
     // do something
 }
 else
 {
     // do something else
 }


Comment
Add comment · Show 2 · 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 dragonking300 · Apr 03, 2017 at 05:43 PM 0
Share

Sorry I forgot to elaborate.

 Vector3 wantedPosition = Player.TransformPoint(0, height, -distance);

is the NullRefrenceException and target is assigned as the Player. I tried to change target = null; to just target but the results are the same

avatar image Commoble dragonking300 · Apr 03, 2017 at 05:52 PM 0
Share

Your Player variable might be null. $$anonymous$$ake sure it's being assigned.

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

96 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 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

Problem with the camera, 2D. 0 Answers

Does anyone know why this simple player camera lock script is not working? 0 Answers

How to adjust camera position/rotation when player looks up or down? (Third Person Camera) 1 Answer

define touch area 2 Answers

How to check if object are link via other collider 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