It looks like this is a misunderstanding of how instances of MonoBehaviour scripts work in Unity.
A MonoBehaviour script is a (non-static) class, which means it's a template for a kind of object you can have in your project - not a unique or global repository. When you add a MonoBehaviour component to a Game Object, what you have created is one instance of that script.
Your project could contain zero instances of the script, or one, or hundreds. Each individual instance gets to have its own set of values stored in its member variables. So setting the variables in the Inspector on one instance does not automatically change the values held in a different instance, or in instances you create in the future.
If I understand correctly what you are doing, in the editor you've added a Lexicon component to a game object, and populated that one instance with the variables you want.
Then at runtime, you have another script that creates a new, empty game object called "Lexicon" (no relation to the object you set your variables on previously), and attaches to that game object a new instance of the Lexicon component. Since this component is new, all of its variable have the default values from the code file. Nothing here tells Unity it should fetch the variables you set in the inspector on that particular other Lexicon component at edit time.
This is a bit like writing a letter, and putting it in an envelope. Then the next day, taking a blank sheet of paper, folding it into an envelope, and opening it - being surprised that this new envelope doesn't contain your letter. Well of course it doesn't, it's not the same envelope you put your letter into!
Your attempt to use GameObject.Find("Lexicon")
will work if the component you set up in the Inspector was on a game object that currently exists in the same scene your script Awake script is running in, and named exactly "Lexicon". This method searches only the current scene, and won't find objects you configured in other, unloaded scenes, or in prefabs in your Assets folder. It's also extremely picky about the name. So if you have a "Lexicon" prefab that you spawn at runtime to make an instance in your scene called "Lexicon (Clone)", the Find
method won't find that one.
Sometimes folks use the singleton pattern to make one globally-accessible instance of a MonoBehaviour script, but singletons have their own complications, and I think we can do better for your use case.
If you just want a data container other scripts can reference, what you might want instead is a ScriptableObject. This is a type like a MonoBehaviour that lets you edit data in the Inspector window, but it doesn't attach to a game object in your scene. Instead, it can exist as a standalone asset in your Assets folder. This makes it useful for authoring shared data that you might want to reference from many scripts or many scenes.
Here's what that might look like:
// This attribute lets us create an instance of this asset via the menus.
[CreateAssetMenu(fileName="Lexicon.Asset", menuName = "Data/Lexicon")]
public class Lexicon : ScriptableObject
{
public Tile thiccTile;
public Tile thiccTile2;
public Tile thiccTile3;
private string fact = "Yummy porchop";
private string favoritefact;
private Tile favoritetile;
// Generally for Unity types we avoid using constructors,
// since object properties are set during deserialization.
// OnEnable or Awake are good substitutes for ScriptableObjects.
void OnEnable() {
favoritetile = thiccTile2;
favoritefact = fact;
}
// It looks like you could implement these as auto-properties for less typing...
public Tile Favoritetile { get { return favoritetile; } set { favoritetile= value; } }
public string Favoritefact { get { return favoritefact; } set { favoritefact= value; } }
The attribute at the top lets us create an instance of this asset type via the Assets-> Create menu or the right-click context menu in the Project window:
Then we get a new asset we can configure in the Inspector:
Note how this data is standalone, not attached to a game object or prefab, and not bound to any scene.
Now any script can hold a reference to this Lexicon by declaring a public variable:
public Lexicon lexicon;
And you can assign it in the Inspector to reference this asset:
This reference will work from any scene, any prefab, etc., and keeps your data centralized in a single asset file rather than duplicated among multiple scenes.
AddComponent<>
call? \$\endgroup\$AddComponent<>
, you get a brand new instance of that component with default values. It can't fetch something you previously set in the Inspector on a different object, because it doesn't know which object it should copy the values from (you could have hundreds of these scripts with different values elsewhere in your project, or zero, for all it knows). \$\endgroup\$