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:
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?
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?
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.
Your answer
Follow this Question
Related Questions
Changing positions of all GameObjects in an array 0 Answers
NullReferenceException when using gameObject.SetActive(false); 0 Answers
How is this Instantiate NullReferenceException? 0 Answers
Help Me on Storing GameObject!!! 1 Answer
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer