• 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 GregPDesUGD · Jun 22, 2016 at 03:17 PM · gameobjectnullreferenceexceptioncamera rotate

NullReferenceException on a GameObject holding the main scene camera as the parent

I am still new to Unity 5 and learning how it works. I am enhancing my Pong clone built on Unity 5 last fall, and there's a problem I'm running into.

In my GameController script, I am referencing an empty GameObject where its child is the main camera from the entire scene. I have it set up this way because I want my board to tilt on its horizontal axis based on how far the ball is up or down relative to the axis.

Because I have my two paddles and the ball move on the x and z axis respectively, it would be totally complicated to solve the problems that build if I was to just rotate the board. So what I’m doing instead is have the main camera pivot with respect to the center of the board instead of the centre.

Even though in my script, I do have a reference variable to refer to the parent of the camera, and I have assigned the empty GameObject to it in the inspector, I am getting the following error messages on the console:

NullReferenceException: Object reference not set to an instance of an object (GameController.cs:44, FixedUpdate())

Here’s the code for my GameController script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameController : MonoBehaviour 
 {
     // Game Objects
     public Player playerBoard; // Player's paddle
     public Opponent opponentBoard; // Opponent's paddle
     public Ball pongBall; // Ball
     public GameObject mainCameraPivot; // Main camera component (for pivot operations)
 
     // Game Tag
     public static bool gameOn = true;
 
 
     // UI Elements
     public Text winText;
     public Text playerScoreText;
     public Text opponentScoreText;
 
     // Player Goals
     byte playerGoals = 0;
     byte opponentGoals = 0;
     const byte goalsToWin = 5;
 
     // Camera Limits
     const float displacementFromCentreOfBoard = 2.45f;
     const float maximumTiltAngle = 15.0f;
 
     // Use this for initialization
     void Start () 
     {
         pongBall = GetComponent<Ball>();
         winText.text = "";
         playerScoreText.text = "Player: 0";
         opponentScoreText.text = "CPU: 0";
     }
     
     // Physics Code
     void FixedUpdate () 
     {
         if (gameOn)
         {
             // Tilt the board back and forth based on the ball's y position.
             mainCameraPivot.transform.rotation = Quaternion.Euler (new Vector3(- maximumTiltAngle * (pongBall.transform.position.z / displacementFromCentreOfBoard), 0.0f));
             
             // This won't work because this function only changes the camera's properties and not keep it static with regards to the pivot's normal axis.
             // Essentially, I want the camera to rotate relative to the axis but with angle limits so that it does not rotate too much. 
             /*Camera.main.transform.RotateAround(Vector3.zero, Vector3.up, Vector3.Angle(Vector3.up, 
                 new Vector3(-maximumTiltAngle * (pongBall.transform.position.z / displacementFromCentreOfBoard), 0.0f)));*/
         }
     }
 
 
     // Helper Functions
     public void incrementPlayerGoals()
     {
         playerGoals++;
         mainCameraPivot.transform.rotation = Quaternion.Euler (new Vector3(0.0f, 0.0f));
         //Camera.main.transform.RotateAround(Vector3.zero, Vector3.up, 0.0f);
         outputPlayerGoals();
     }
     
     public void outputPlayerGoals()
     {
         playerScoreText.text = "Player: " + playerGoals.ToString();
     }
 
     public void incrementOpponentGoals()
     {
         opponentGoals++;
         mainCameraPivot.transform.rotation = Quaternion.Euler (new Vector3(0.0f, 0.0f));
         //Camera.main.transform.RotateAround(Vector3.zero, Vector3.up, 0.0f);
         outputOpponentGoals();
     }
     
     public void outputOpponentGoals()
     {
         opponentScoreText.text = "CPU: " + opponentGoals.ToString();
     }
 
     public bool playerWins()
     {
         return playerGoals == goalsToWin;
     }
 
     public bool opponentWins()
     {
         return opponentGoals == goalsToWin;
     }
 }
 

The error line is Line 46, which is this:

 mainCameraPivot.transform.rotation = Quaternion.Euler (new Vector3(- maximumTiltAngle * (pongBall.transform.position.z / displacementFromCentreOfBoard), 0.0f));

Please help me out here! What is it I'm missing?

cusersgregpdocumentsuniversity-of-waterloo-co-oper.png (36.0 kB)
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 Grhyll · Jun 22, 2016 at 03:58 PM 0
Share

First of all, it would help if you selected an error line to have the detail of the error (the exact line where the null ref occurs). Secondly, did you correctly assign each of your public variables (that aren't retrieved by code) in the editor?

1 Reply

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

Answer by GregPDesUGD · Jun 22, 2016 at 06:41 PM

Actually, wait a minute! I just found out where the error is coming from. I did not assign my Ball game object to this controller in the Editor.

Now with this in, the camera pivot action works.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

67 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

Related Questions

Null Reference Exception ONLY (sometimes) when loading the scene... 1 Answer

NullException Error while instantiating gameobjects 0 Answers

(Solved) Findout if an object exist in the scene, but dont trow exception 1 Answer

Grenade damage script error 0 Answers

Unity 5 NullReferenceException after loading new scene. 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