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();
}
}
}
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;
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
Follow this Question
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