I am working on this code that will enable me to move the camera forward or back based on a button press handled by another class. Here is the code that handles the camera to move:
using UnityEngine;
using System.Collections;
public class NavigationalCamera : MonoBehaviour {
public float moveSpeed;
void Update()
{
NavigateForward();
}
[HideInInspector]
public void NavigateForward()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
if (Camera.main.transform.position.z >= 60)
{
moveSpeed = 0.0f;
}
}
}
This is the class I am using to call it:
using UnityEngine;
using System.Collections;
///
/// This class handles the user's background info
///
[ExecuteInEditMode]
public class myTestClass: MonoBehaviour {
NavigationalCamera obj_navCamera;
//Used for storing the size and position of the backdrop window
public Rect GUIRectWindow;
public Rect btn_Continue;
/// <summary>
/// Initializes all variables
/// </summary>
void Awake()
{
obj_navCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<NavigationalCamera>();
}
public void OnGUI()
{
myMainWindow();
}
/// <summary>
/// Displays the background survey that the user will fill out
/// </summary>
public void myMainWindow()
{
// Make a backgroound box and center it
GUI.Box(GUIRectWindow, "Press the Button");
//set up an action when the user clicks on the button
if (drawButon(btn_Continue, "Continue"))
{
obj_navCamera.NavigateForward();
}
}
/// <summary>
/// Draws a button
/// </summary>
/// <param name="rect"></param>
/// <param name="buttonName"></param>
/// <returns></returns>
bool drawButon(Rect rect, string buttonName)
{
if (GUI.Button(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), buttonName))
{
return true;
}
return false;
}
}
When I run this in the game and press the button it throws me a null reference exception and for the life of me I just can’t get it to work. Can anyone help me figure out what to do in order to fix it? Thank you in advance!