- Home /
Duplicate Question
Null Reference Exception
I am following the unity tutorials in the book http://book.prototools.net/ (great book by the way) and I keep getting an error at one part in the second project. Both of the following scripts are attached to the main camera. The ViewBoth object is not null and it is in the scene. I receive the error:
NullReferenceException: Object reference not set to an instance of an object MissionDemolition.SwitchView (System.String eView) (at Assets/__Scripts/MissionDemolition.cs:124) MissionDemolition.StartLevel () (at Assets/__Scripts/MissionDemolition.cs:50) MissionDemolition.Start () (at Assets/__Scripts/MissionDemolition.cs:33)
Mission Demolition Script:
using UnityEngine;
using System.Collections;
public enum GameMode{
idle,
playing,
levelEnd
}
public class MissionDemolition : MonoBehaviour {
static public MissionDemolition S; //singleton
public GameObject[] castles;
public GUIText gtLevel;
public GUIText gtScore;
public Vector3 castlePos;
public bool ___________________________;
public int level;
public int levelMax;
public int shotsTaken;
public GameObject castle;
public GameMode mode = GameMode.idle;
public string showing = "Slingshot";
// Use this for initialization
void Start () {
S = this;
level = 0;
levelMax = castles.Length;
StartLevel ();
}
void StartLevel(){
if (castle != null) {
Destroy (castle);
}
GameObject[] gos = GameObject.FindGameObjectsWithTag ("Projectile");
foreach (GameObject pTemp in gos) {
Destroy (pTemp);
}
castle = Instantiate (castles [level]) as GameObject;
castle.transform.position = castlePos;
shotsTaken = 0;
SwitchView ("Both");
ProjectileLine.S.Clear ();
Goal.goalMet = false;
ShowGT ();
mode = GameMode.playing;
}
void ShowGT(){
gtLevel.text = "Level: " + (level + 1) + " of " + levelMax;
gtScore.text = "Shots Taken: " + shotsTaken;
}
// Update is called once per frame
void Update () {
ShowGT ();
if (mode == GameMode.playing && Goal.goalMet) {
mode = GameMode.levelEnd;
SwitchView("Both");
Invoke ("NextLevel", 2f);
}
}
void NextLevel(){
level++;
if (level == levelMax) {
level = 0;
}
StartLevel ();
}
void OnGUI(){
Rect buttonRect = new Rect ((Screen.width / 2) - 50, 10, 100, 24);
switch (showing) {
case "Slingshot":
if(GUI.Button (buttonRect,"Show Castle")){
SwitchView("Castle");
}
break;
case "Castle":
if(GUI.Button (buttonRect,"Show Both")){
SwitchView("Both");
}
break;
case "Both":
if(GUI.Button (buttonRect,"Show Slingshot")){
SwitchView("Slingshot");
}
break;
}
}
static public void SwitchView(string eView){
S.showing = eView;
switch (S.showing) {
case "Slingshot":
FollowCam.S.poi = null;
break;
case "Castle":
FollowCam.S.poi = S.castle;
break;
case "Both":
if(GameObject.Find ("ViewBoth") != null){
print ("not null");
print (GameObject.Find ("ViewBoth").tag);
}else{
print("null");
}
GameObject both = GameObject.Find ("ViewBoth");
FollowCam.S.poi = both;
//FollowCam.S.poi = GameObject.Find("ViewBoth");
break;
}
}
public static void ShotFired(){
S.shotsTaken++;
}
}
Follow Cam Script:
using UnityEngine;
using System.Collections;
public class FollowCam : MonoBehaviour {
static public FollowCam S; //singleton
public float easing = 0.05f;
public Vector2 minXY;
public bool _________________________;
public GameObject poi; //point of interest
public float camZ; //desired z position of the camera
// Use this for initialization
void Start () {
S = this;
camZ = this.transform.position.z;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 destination;
if (poi == null) {
destination = Vector3.zero;
} else {
destination = poi.transform.position;
if(poi.tag == "Projectile"){
if(poi.rigidbody.IsSleeping()){
poi = null;
return;
}
}
}
destination.x = Mathf.Max (minXY.x, destination.x);
destination.y = Mathf.Max (minXY.y, destination.y);
destination = Vector3.Lerp (transform.position, destination, easing);
destination.z = camZ;
transform.position = destination;
this.camera.orthographicSize = destination.y + 10;
}
}
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Working Reference Still Throwing Null Reference Error 0 Answers