Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 carnivoris · Dec 14, 2014 at 03:43 PM · cameranullreferenceexceptioncamera.main

I'm getting a NullReferenceException on Camera.main

The camera in question is using the MainCamera tag, but when I try to use Camera.main, I get a null reference exception and I can't figure out why. Could someone enlighten me?

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LootPickup : MonoBehaviour {
     
     public GameObject ak47;
     public GameObject m4a1;
     public Transform gunPoint;
     //public GameObject crosshairUIobj;
     Camera myCamera;
 
     //Text lootText;
     GameObject Player;
     // Weapons:
     // 0 = Unarmed
     // 1 = m4a1
     // 2 = ak47
     int newWeapon = 0;
     
     // Use this for initialization
     void Start () {
         Player = GameObject.Find ("Player Controller(Clone)");

         //lootText = crosshairUIobj.GetComponent<Text> ();
     }
     
     // Update is called once per frame
     void Update () {
         Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
         Transform hitTransform;
         Vector3 hitPoint;
         
         hitTransform = FindClosestHitObject(ray, out hitPoint);
         
         //Debug.Log (hitTransform.gameObject.tag);
             if(hitTransform.transform.CompareTag ("ak47")){
                 newWeapon = 2;
             //lootText.text = "Pick up AK-47";
             }
             if(hitTransform.transform.CompareTag ("m4a1")){
                 newWeapon = 1;
             //lootText.text = "Pick up M4A1";
             }
         if(Input.GetKeyDown("f"))
         {
             exchangeWeapon (newWeapon);
         }
     }
 
     void exchangeWeapon(int newWeapon){
 
         if (newWeapon == 1) {
             ak47.gameObject.SetActive(false);
             m4a1.gameObject.SetActive(true);
         }
         if (newWeapon == 2) {
             ak47.gameObject.SetActive(true);
             m4a1.gameObject.SetActive(false);
         }
         if (newWeapon == 0) {
             ak47.gameObject.SetActive(false);
             m4a1.gameObject.SetActive(false);
         }
     }
 
     Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint) {
         
         RaycastHit[] hits = Physics.RaycastAll(ray);
         
         Transform closestHit = null;
         float distance = 0;
         hitPoint = Vector3.zero;
         
         foreach(RaycastHit hit in hits) {
             if(hit.transform != this.transform && ( closestHit==null || hit.distance < distance ) ) {
                 // We have hit something that is:
                 // a) not us
                 // b) the first thing we hit (that is not us)
                 // c) or, if not b, is at least closer than the previous closest thing
                 
                 closestHit = hit.transform;
                 distance = hit.distance;
                 hitPoint = hit.point;
             }
         }
         
         // closestHit is now either still null (i.e. we hit nothing) OR it contains the closest thing that is a valid thing to hit
         
         return closestHit;
         
     }
 }

This is the console window output:

 NullReferenceException: Object reference not set to an instance of an object
 LootPickup.Update () (at Assets/LootPickup.cs:32)
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
6
Best Answer

Answer by Bunny83 · Dec 14, 2014 at 03:53 PM

"The first enabled camera tagged 'MainCamera'". You can be sure that this method works exactly as stated here. So if it returns null it's either not tagged MainCamera, not enabled on a GameObject that is not active or simply doesn't exist (anymore?).

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 carnivoris · Dec 14, 2014 at 04:20 PM 0
Share

Yeah, that was it. The character is instantiated with PUN so the camera wasn't there yet. I disabled this script and enabled it in code after the camera enabling code and it's all good.

avatar image DDeathlonger · Aug 21, 2018 at 02:47 AM 0
Share

I always forget to run things in OnPhotonInstantiate ins$$anonymous$$d of Start... XD

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

28 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

Related Questions

NullReferenceException in game build 1 Answer

Converting to 3rd Person from 1st Person - Island Demo 1 Answer

Full screen camera missing? 3 Answers

NullReferenceException 2 Answers

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