- Home /
Have two scripts modify eachother
I have two scripts - an inventory script attached to my player and an item database (which is giving me trouble). The database stores icons and models. I want to be able to instantiate the database object in my inventory script and pull up icons and models from the instance. How do I go about this?
The problem is that instantiating the database with a constructor seems to circumvent the Start() method and the public fields of the script I fill in the editor. So I end up with all my images and models being null. I could have the database build itself when the constructor is called but dragged-in references to my files don't seem to stick with the instance and also end up being null when I hit play. I tried using Resources.Load
in my constructor and even though everything worked Unity threw angry errors about main threads and Load calls - I'd like to avoid those if I could.
So what is the right way of doing this?
There's really not much to show - this is more of a design issue but here is an abridged version:
using UnityEngine;
using System.Collections.Generic;
public class ItemDatabase : $$anonymous$$onoBehaviour
{
public Texture2D icon;
public Texture2D getIcon()
{
return icon;
}
}
And here is the other one:
using UnityEngine;
using System.Collections.Generic;
public class Inventory : $$anonymous$$onoBehaviour
{
//now this is where I don't know what to do...
//the following line wont work because I need an instance to call a method from ItemDatabase, and I haven't instantiated it
public Texture2D itemIcon = ItemDatabase.getIcon();
//ok, so lets instantiate it...
public ItemDatabase itemDB = new ItemDatabase();
//but now I need to make a constructor in ItemDatabase and I don't know how to tell it to use what I dragged into the icon field of the ItemDatabase script.
}
So you want to have the ItemDatabase created as a Singleton, which is also a $$anonymous$$onoBehaviour that you can attach to a game object and add all the Textures etc in through the editor interface.
In this way, there is only ever one copy of the database, and you can reference it directly like so:
public Texture2D itemIcon = ItemDatabase.Instance.getIcon();
If you're not sure what I'm talking about, you can check out these links:
Answer by sparda928 · Nov 16, 2013 at 11:52 PM
Ok so the another solution aside from what VioKyma wrote (which is awesome by the way) was to remove any and all constructors (general rule for all MonoBehaviours
). Then have the ItemDatabase
class build its database using Resources.Load
in the Awake()
method (which is always called before the Start()
method). Then just drag the ItemDatabase
script attached to the same object into the editor's ItemDatabase variable field (which you need to publicly define in the Inventory
class). This ensures that before Start()
is called in Inventory the database has already been built...works like a charm.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Best practice for assigning class reference at runtime? 0 Answers
Referencing a script works... but not on iOS 0 Answers
Getting NullReferenceException? info inside 1 Answer
How do I reference variables from other class files? 1 Answer