title: Unity - Set an instance of a script to use variables from a class Update function in another script
body: <p>This is my first unity project and also my first post, so please comment if I neeed to include further information or anything of the sort.</p>
  
  <p>I'm currently trying to build a minigame where all the information gets automatically saved and appended in a csv-file after the Save method gets called inside the IncreaseScore method, and before the scene automatically changes.
  Saving my score, and filling the other cells with information such as the currentLevel the score was obtained in went smooth. But now I need to save the final cordinate variables from another class that constantly gets updated inside its Update method and I'm stuck. It' s important that its the final updated value for each variable, since the cordinate variables are based on the left HTC Vive hand controll position, and I need to calculate the total movement.
  Clearly my understanding of creating an instance of another class/the update method is too poorly, since I couldn't figure out how to solve it from the other posts.
  Below is my code and the error message I get, thank you very much for any help provided.</p>
  
  <p>Error Messagee: 
  "NullReferenceException: Object reference not set to an instance of an object
  GameManager.IncreaseScore (Int32 amount) (at Assets/Scripts/GameManager.cs:159)
  SwordCutter.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/SwordCutter.cs:53)"</p>
  
  <p>I understand I need to create an instance of the LeftControllerTracking class somehow, but I'm not sure how to do that, and where.The provided LeftControllerTracking script below is attached to the GameObject ControllerLeft in the inspector.
  I've also dragged the ControllerLeft GameObject to the GameManager object with the GameManager script in the inspector view.</p>
  
  <p>My GameManager class:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  using UnityEngine.SceneManagement;
  using System;
  using System.Runtime.Serialization.Formatters.Binary;
  using System.IO;
  using System.Text;
  
  public class GameManager : MonoBehaviour
  {
      //the new part
      public GameObject leftposition;
      LeftControllerTracking leftController;
  
      public List&lt;string[]&gt; rowData = new List&lt;string[]&gt;();
  
      // Player score
      public int score = 0;
  
      // High score
      public int highScore = 0;
  
      // Static instance of the Game Manager,
      // can be access from anywhere
      public static GameManager instance = null;
  
      // Level, starting in level 1
      public int currentLevel = 1;
  
      // Highest level available in the game
      public int highestLevel = 3;
  
      // Called when the object is initialized
      void Awake()
      {
  
          // if it doesn't exist
          if (instance == null)
          {
              print("assigning GameManager instance");
              // Set the instance to the current object (this)
              instance = this;
          }
  
          // There can only be a single instance of the game manager
          else if (instance != this)
          {
              print("GameManager instance already exists");
              // Destroy the current object, so there is just one manager
              Destroy(gameObject);
              return;
          }
  
          // Don't destroy this object when loading scenes
          DontDestroyOnLoad(gameObject);
  
      }
  
  
      private void Update()
      {
          //new part that is missplaced and can't be accessed anyway it seems
          //leftX = leftController.finalMovementX;
          //leftY = leftController.finalMovementY;
          //leftZ = leftController.finalMovementZ;
      }
  
      // Need the finalMovement variables as argument here
      public void Save(int score, int currentLevel, float leftX, float leftY, float leftZ)
      {
  
          // Creating First row of titles manually the first time i use the script
          string[] rowDataTemp = new string[7];
          rowDataTemp[0] = "Name";
          rowDataTemp[1] = "ID";
          rowDataTemp[2] = "Lvl";
          rowDataTemp[3] = "Score";
          rowDataTemp[4] = "X Movement";
          rowDataTemp[5] = "Y Movement";
          rowDataTemp[6] = "Z Movement";
          //rowDataTemp[3] = "Total Movement";
          rowData.Add(rowDataTemp);
  
          // You can add up the values in as many cells as you want.
          for (int i = 0; i &lt; 1; i++)
          {
              rowDataTemp = new string[7];
              rowDataTemp[0] = "Andreas"; // name
              rowDataTemp[1] = "1"; // ID
              rowDataTemp[2] = "" + currentLevel; // Score
              rowDataTemp[3] = "" + score; // Score
              rowDataTemp[4] = "" + leftX; // X Movement
              rowDataTemp[5] = "" + leftY; // Y Movement
              rowDataTemp[6] = "" + leftZ; // Z Movement
  
              rowData.Add(rowDataTemp);
  
          }
  
          string[][] output = new string[rowData.Count][];
  
          for (int i = 0; i &lt; output.Length; i++)
          {
              output[i] = rowData[i];
          }
  
          int length = output.GetLength(0);
          string delimiter = ",";
  
          StringBuilder sb = new StringBuilder();
  
          for (int index = 0; index &lt; length; index++)
              sb.AppendLine(string.Join(delimiter, output[index]));
  
          string filePath = getPath();
  
          //switch to System.IO.File.AppendText(filePath); the second time you use the script
          StreamWriter outStream = System.IO.File.CreateText(filePath);
          outStream.WriteLine(sb);
          outStream.Close();
  
      }
  
      // Following method is used to retrive the relative path as device platform
      private string getPath()
      {
          #if UNITY_EDITOR
          return Application.dataPath + "/CSV/" + "Saved_data.csv";
          #elif UNITY_ANDROID
          return Application.persistentDataPath+"Saved_data.csv";
          #elif UNITY_IPHONE
          return Application.persistentDataPath+"/"+"Saved_data.csv";
          #else
          return Application.dataPath +"/"+"Saved_data.csv";
          #endif
      }
  
  
      // Increase score
      public void IncreaseScore(int amount)
      {
          // Increase the score by the given amount
          score += amount;
  
          // Show the new score in the console
          print("New Score: " + score.ToString());
  
          if (score &gt; highScore)
          {
              highScore = score;
              print("New high score: " + highScore);
          }
  
          if (score &gt; 24)
          {
              // Increase level
              Save(score, currentLevel, leftController.finalMovementX, leftController.finalMovementY, leftController.finalMovementZ);
              GameManager.instance.IncreaseLevel();
          }
      }
  
      // Restart game. Refresh previous score and send back to level 1
      public void Reset()
      {
          // Reset the score
          score = 0;
  
          // Set the current level to 1
          currentLevel = 1;
  
          // Load corresponding scene (level 1 or "splash screen" scene)
          SceneManager.LoadScene("Level" + currentLevel);
      }
  
      // Go to the next level
      public void IncreaseLevel()
      {
          if (currentLevel &lt; highestLevel)
          {
              currentLevel++;
          }
          else
          {
              currentLevel = 3;
          }
          SceneManager.LoadScene("Level" + currentLevel);
      }
  }
  </code></pre>
  
  <p>My LeftControllerTracking that is attached to my ControllerLeft gameobject:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class LeftControllerTracking : MonoBehaviour {
  
      public Transform track;
      public Transform cachedTransform;
      public Vector3 cachedPosition;
      public float finalMovementX = 0;
      public float finalMovementY = 0;
      public float finalMovementZ = 0;
  
      void Start()
      {
          if (track)
          {
              cachedPosition = track.position;
          }
      }
  
      public void Update()
      {
          if (track &amp;&amp; cachedPosition != track.position)
          {
              finalMovementX = (finalMovementX + (track.position.x - cachedPosition.x));
              finalMovementY = (finalMovementY + (track.position.y - cachedPosition.y));
              finalMovementZ = (finalMovementZ + (track.position.z - cachedPosition.z));
  
              print("New Total Left Katana X cordinate: " + finalMovementX);
              print("New Total Left Katana Y cordinate: " + finalMovementY);
              print("New Total Left Katana Z cordinate: " + finalMovementZ);
  
              cachedPosition = track.position;
              transform.position = cachedPosition;
          }
      }  
  }
  </code></pre>