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 VAN-D00M · Jul 17, 2014 at 07:50 PM · c#error

Error NullReferenceException: Object reference not set to an instance of an object

Hi

It was working, then this error just came out of nowhere. Don't understand whats going on.

Its a top down shooter and the idea being that it aims and shoots using the curser.

Can anyone offer any suggestions please. Really am stuck with this one. :)

 public class PlayerController : MonoBehaviour 
 {
     //handling
     public float rotationSpeed = 1000;
     public float speed = 20;
     public float fireRate;
 
     private float nextFire;
 
 
 
     //system
     private Quaternion targetRotation; //stores direction
     public PlayerBoundary boundary;
 
 
     //components
     private CharacterController controller;
     private Camera cam;
 
     public GameObject shot;
     public Transform shotSpawn;
 
 
     
     
     void Start()
     {
         controller = GetComponent<CharacterController> ();
         cam = Camera.main;
         
     }
     
 
     void Update()
     {

     ControlMouse ();
           
         if (Input.GetButton ("Shoot") && Time.time > nextFire) 
         {
             nextFire = Time.time + fireRate;
             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
 
             audio.Play ();
 
 
         }
     }
 
 
     
 
 
 
     void ControlMouse()
     {
 
 
 
         //set up direction in input variabe
         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"),0, Input.GetAxisRaw("Vertical")); 
 
         //Sets up mouse position
         Vector3 mousePos = Input.mousePosition;
         mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
         shotSpawn.LookAt(mousePos);
 
 
 
         if (input != Vector3.zero) //fingers taken off controls, player doesnt reset to defaul position
         {
             targetRotation = Quaternion.LookRotation(input); //rotates player in the way of the direction
             transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
         }
 
         //Controls Movement 
         Vector3 motion = input;
         motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs (input.z) == 1) ? .7f : 1; //moving diagonally doesnt travel at twice the speed
 
         controller.Move (motion * speed * Time.deltaTime);
 
 
 
     }
 
 
 
 
 
 }



Comment
Add comment · Show 2
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 · Jul 17, 2014 at 07:51 PM 0
Share

Please indicate which line is generating the error. I see several places that have the potential..

avatar image VAN-D00M · Jul 17, 2014 at 08:30 PM 0
Share

It doesn't like line 65-66.

1 Reply

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

Answer by Loius · Jul 17, 2014 at 09:57 PM

this is a duplicate question. did you search for your error message? i've answered this the same way several times now, and that's just me.

null reference exception: "something before a . is null".

Line 66 (of 65-66, assuming your line numbers in this code are correct), only has one thing before a . that can be null, and that is 'cam'. Since you set cam = Camera.main in start, either line 66 is called before start (during awake), or you have no camera that is tagged "MainCamera".

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Error: error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject 1 Answer

error CS0246 anybody help? 1 Answer

C# damage script help 1 Answer

error cs0236 field initializer cannot reference a nonestatic field, method or property 1 Answer


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