Error CS1061: Are you missing an assembly reference?
Hello, Im getting the following error. Im trying to reference the text from the Scriptable scripts class and Im getting the following 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?
The code
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(); //THIS IS WHERE THE ERROR SHOWS. Here .text is not shown in the auto completion.
thisText.text = randomStringSelected.toString();
}
}
}
public static class ArrayExtensions
{
public static T RandomItem<T>(this T[] array)
{
return array[Random.Range(0, array.Length)];
}
}
This is my 3rd post about this error. The user @Hellium keeps deleting it. I did try using
script.info[index].text.RandomItem();
as suggested by him. But I get the error
Assets/Scripts/handler.cs(84,4): error CS0103: The name `index' does not exist in the current context
Simply because you have declared script.info
as an array, and thus, you must access an info using an index ! Simply declare an index somewhere and set its value or declare script.info
as a simple instance of the Info
class.
And, for you information :
FAQ :
Some reasons for getting a post rejected:
Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue. Also, take a look at the Unity's support website, many errors and how to solve them are described here
You are wrong about the index part :/ RandomItem() is returning an Info item so no index is needed and the .text should be in the end as it is the field you are trying to reach.
Answer by PizzaPie · Mar 18, 2017 at 07:21 PM
randomStringSelected = script.info.RandomItem().text;
replace the line with the error with this^^. cheers.
Answer by Commoble · Mar 18, 2017 at 07:32 PM
Your Scriptable class is a ScriptableObject. ScriptableObjects are not Components. ScriptableObjects cannot be attached to GameObjects, nor can they be retrieved with GetComponent.
To get a reference to a scriptable object, add [CreateAssetMenu] to your class declaration, like this:
[CreateAssetMenu]
public Scriptable : ScriptableObject
This adds your Scriptable class to Unity's create asset menu (ScriptableObjects are user-defined assets). You can now right-click in your asset folders to create an instance of your Scriptable asset, which can then be dragged into your handler component's inspector field.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113444im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Memory problem on android/ios 1GB ram with string[]? 0 Answers
how to connect my Health to my UI 1 Answer
trying to get an Array string to read an Int 0 Answers
Storing input to array 1 Answer