Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jan 05, 2015 at 11:57 AM by Graham-Dunnett for the following reason:

Duplicate Question

avatar image
0
Question by Dando18 · Jan 05, 2015 at 11:57 AM · c#nullreferenceexception

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;
     }
 }
 


Comment
Comments Locked
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Working Reference Still Throwing Null Reference Error 0 Answers

NullReferenceException After A Few Hours? 1 Answer

C# Null Reference Exception in Custom Function 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