Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by joelfernandes23 · Mar 13, 2017 at 02:03 PM · uistringclassscriptableobjectclasses

Accessing scriptable object classes class from other script

Hello, How do I access a scriptable object classes class from other script. Im trying to get the string[] and select randomly array from it and display it as a text.

 using UnityEngine;
     using System.Collections;
     using UnityEngine.UI;
     
     public class Scriptable : ScriptableObject {
     
         #region singleton
         public static Scriptable instance;
         public static Scriptable Instance
     {
         get{if(instance == null) instance = Resources.Load("Scriptable") as 
     
     Scriptable; return instance;}
     }
         #endregion
     
         [System.Serializable]
              public  class Info{
                public string text;
             [Space()]
             public float price = 0f;
             public bool somethingElse = false;
             public bool somethingMore = false;
             public bool somethingEvenMore = false;
             public bool thisIsSomething = false;
             public bool somethingSomething = false;
        }
          public  Info[] info;
  }

and the script where I access it with is here

  using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEngine.UI;
     
     public class handler : MonoBehaviour {
     
                     public Scriptable script;
                     public Text thisText; 
                     public string randomStringSelected;
   
        void Start () {
        Scriptable = transform.GetComponent<Scriptable> ();
     
             if (script)
                 {
                        randomStringSelected = script.info.text.RandomItem();
                       thisText.text = randomStringSelected.toString();
     }
 
 
 }
 
 }
 


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Commoble · Mar 13, 2017 at 06:25 PM

Well, firstly, ScriptableObjects aren't components, and therefore you can't get them with GetComponent; they're essentially user-defined Asset types. Components are attached to GameObjects, while ScriptableObjects are never attached directly to GameObjects -- you create instances of your ScriptableObject type, and then those instances exist as individual files, and you can link them to your scripts like you would a texture or an audio file.

The easiest way to make an instance of your Scriptable asset type is to add [CreateAssetMenu] above the class definition, like this:

 [CreateAssetMenu]
 public class Scriptable : ScriptableObject {
     // etc

and then you can right-click in one of your asset folders and the option to create a Scriptable asset will be there, and you can edit that asset from the inspector.

Now, you have an asset file of your Scriptable asset type. Since your handler class has the field public Scriptable script;, you can drag your Scriptable asset to that field in that script's inspector, and now your script has a reference to that asset. Now your Start function can look like:

 void Start()
 {
     if (this.script != null)
     {
         this.randomStringSelected = this.script.info.text.RandomItem(); // see below
         this.thisText = randomStringSelected.ToString();
     }
 }

Mind you, the line I marked in that (copied from your code sample) is incorrect, and has several problems with it. To get a random string from your Scriptable asset, this is one way you could do that:

 this.randomStringSelected = this.script.info[UnityEngine.Random.Range(0, this.script.info.Length)].text;
Comment
Add comment · Show 1 · 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
avatar image joelfernandes23 · Mar 13, 2017 at 07:10 PM 0
Share

Hello, Thanks for the reply, I get this error

 error CS1061: Type `Scriptable.Info[]' does not contain a definition for `text' and no extension method `text' of type `Scriptable.Info[]' could be found. Are you missing an assembly reference?

and for choosing Randomly from an array I had used this

 public static class ArrayExtensions
 {
 
     public static T RandomItem<T>(this T[] array)
     {
         return array[Random.Range(0, array.Length)];
     }
 }

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.

Follow this Question

Answers Answers and Comments

132 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 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

How to reference a class in an enumerator 1 Answer

I can not use classes 1 Answer

Error CS1061: Are you missing an assembly reference? 2 Answers

Custom type Objects without hte " = new " 2 Answers

Custom class, Null Reference Exception 4 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