• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LoKko_777 · Sep 30, 2018 at 09:42 PM · getcomponentscriptableobjectaccessing from any scriptcs0120

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.

alt text

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?

sin-titulo.png (8.5 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Sep 30, 2018 at 09:55 PM 0
Share

what does "It needs to be changed one by one" mean?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Issues accessing a variable in another script 0 Answers

How to access an AudioClipVariable from other Script c# 0 Answers

Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object 2 Answers

How do I take a public float from a different script and access it 0 Answers

How do I acces a specific GameObject within the terrain???? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges