Answers for "unity c# static access non static"

C#
0

unity c# static access non static

// To access a non static variable from a static function create a static instance

public class ExampleClass : MonoBehaviour 
{
  static ExampleClass instance;
  
  string NonStaticVariable = "Hello";
  
  void Awake () 
  {
    // Assign the instance to this class:
  	instance = this; 
  }
  
  static void DoSomething () 
  {
  	// Write the value of NonStaticVariable to the console:
    Debug.Log(instance.NonStaticVariable);
  }
}
Posted by: Guest on October-27-2020

C# Answers by Framework

Browse Popular Code Answers by Language