Guest User

Static

a guest
Mar 16th, 2015
258
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /// <summary>
  5. /// Place this on GameObject in scene 1
  6. /// </summary>
  7. public class StaticScript : MonoBehaviour {
  8.  
  9.     public static StaticScript Instance = null;
  10.  
  11.     // Use this for initialization
  12.     void Start () {
  13.    
  14.         Debug.Log (string.Format("{0} was created in scene {1}", this.gameObject.name, Application.loadedLevelName));
  15.         Instance = this;
  16.  
  17.         Debug.Log ("Loading scene 2");
  18.  
  19.         Application.LoadLevel("2");
  20.     }
  21.    
  22.     // Update is called once per frame
  23.     void Update () {
  24.    
  25.     }
  26. }
  27.  
  28. /// <summary>
  29. /// Place this on GameObject in scene 2
  30. /// </summary>
  31. public class AnotherScript : MonoBehaviour {
  32.    
  33.     // Use this for initialization
  34.     void Start () {
  35.         Debug.Log (string.Format("{0} was created in scene {1}", this.gameObject.name, Application.loadedLevelName));
  36.         Debug.Log ("Accessing StaticObject that was created in previous scene");
  37.        
  38.         var staticObject = StaticScript.Instance;
  39.        
  40.         Debug.Log ("StaticObject: " + staticObject);
  41.         Debug.Log ("GameObjet name of StaticObject: " + staticObject.gameObject.name); // Fail
  42.     }
  43.    
  44.     // Update is called once per frame
  45.     void Update () {
  46.        
  47.     }
  48. }
RAW Paste Data