- Home /
Create Array Accessible By Any Script
Hello, this is probably a stupid question. But what I want to do is create some sort of Global Array that i can access from any script, because I made a grid and i need to add which grid segments i have clicked on to the array. Thanks if you know some way of doing this.
Answer by markpdolby · Jul 12, 2013 at 09:44 AM
You could use a static variable:
public class MyClass
{
public static bool[] MyArray;
}
public class MySecondClass
{
void ExampleMethod()
{
MyClass.MyArray;
}
}
So you can now access that variable from any script in your project by refering to the class name and then the variable name. Hope that makes sense
The other option is using the singleton pattern:
public class MyClass
{
public static Instance {get {return instance}}
private static instance;
public bool[] MyArray;
void Awake(){
instance = this;
}
}
public class MySecondClass
{
void ExampleMethod()
{
MyClass.Instance.MyArray;
}
}
Your answer
Follow this Question
Related Questions
is it better to use MeshFilter[] or three separate MeshFilter var's 1 Answer
EnemyAi script 1 Answer
Inventory moving items bug. 0 Answers
How to make a Jagged Array global? 1 Answer
Referencing an instantiated object with a global variable 0 Answers