Game Development Reference
In-Depth Information
var mainScript =
(MainScript)UnityEngine.GameObject.FindObjectOfType(typeof(MainScript));
This will give you a reference to an instance of a class of the MainScript type in your
current scene.
Note
All the functions are not scene aware. So, if you have to access the items in specific
scenes, you will need to track that manually through a static property or through events
(refer to the next section).
Additionally, if your script is used on several objects in the scene, be sure to perform the
search from an instance of an object.
The static classes
The second method to access your Unity project from .NET platforms (such as Windows)
is to expose specific variables outside the confines of your game or app.
To do this, declare a static variable within a class in your project, as shown in the follow-
ing example:
public class MyExternalCass : MonoBehaviour
{
public static bool TurnOnAds;
}
Then, from your project, once you have built it, you can access this variable using the fol-
lowing code:
MyExternalClass.TurnOnAds = false;
You will then have the logic in your game to make use of these variables.
Note
It is highly recommended that you use classes that are single scripts to expose variables in
this fashion. Do not use this method on the reusable classes.
Search WWH ::




Custom Search