- Home /
Accessing to a Scriptable object from another script
So this questions seems silly but I have been for hours trying to find what's exactly the problem here:
I Want to use scriptable objects to store information, mainly string variables, that can be accessed by other objects.
I can easily call those scriptable objects from another script, but I have to select in them manually the scriptable object from the inspector.
The problem I found here is that if I have multiple scripts that want to access to this scriptable object it needs to be changed one by one (and I would like it to be automatically). That's why I am trying to create a script from the game Manager and from it select the scriptable object so all the other scripts will automatically redirected to the scriptable object selected on the Game Manager.
Now, the problem is the following:
This would be the scriptable object:
public class TheCase: ScriptableObject {
public string var01;
}
the script from the Game Manager:
public class Script00: MonoBehaviour {
public TheCase theCase;
public string var01;
public void Start()
{
var01= TheCase.var01;
}
And this would be the script trying to access the String variable:
public class Script01: MonoBehaviour {
public GameManager theGameManager;
public Text var01Text;
void Start () {
theGameManager.GetComponent<Script01>();
var01Text.text = Script00.var01;
}
Now the problem that appears is the following: error CS0120: An object reference is required to access non-static member
I've been researching and the only way that I can get rid of this problem is the changing the following:
changing this:
var01Text.text = Script01.var01;
for this:
var01Text.text = GetComponent<Script00>().var01;
But when I try the game, it doesn't pick up the string.
¿What am I doing wrong?
Sorry, what I mean is that, in order to call the scriptable object, I have to select it manually as an asset. If I link the Scriptable object to another single script, and this is called by all the other gameobjects, I would avoid to be doing that and would be automatically updated everytime I change the scriptable object.
Answer by LoKko_777 · Oct 01, 2018 at 05:47 AM
Okay, just found the answer. I didn't had to refer the gameObject of the Game Manager but the script00 by itselft. Also, I found that my question was more related to inheritance from this post:
https://answers.unity.com/questions/1415831/inheritance-from-a-scriptableobject.html
sorry for the inconvenience
Your answer

Follow this Question
Related Questions
Issues accessing a variable in another script 0 Answers
How to access an AudioClipVariable from other Script c# 0 Answers
how can i test to see if there are more than one objects with a tag C# 1 Answer
ScriptableObject assets don't show in inspector randomly. 0 Answers
PC Build is not behaving like my android and webgl builds 1 Answer