1

I'm not sure how else to word this question. so I will post screenshots of my problem.

How to I make

[SerializeField] private Sprite[] m_Sprites;

show up in the inspector here

enter image description here

I have tried public Sprite[] Sprites; and private Sprite[] m_Sprites as well with no effect, I've seen variables appear there but I cannot seem to figure it out now that I need it. After Further Attempts I have found that a public Sprite Sprite; appears however I require it to be an array of Sprites. I also tried creating a holder class and defining the array inside there and it still doesn't appear.

4
  • does your singleton<Tilemanager> inherit from monobehaviour or scriptableobject or is it a custom class? Commented Apr 15, 2020 at 3:42
  • Also is there a specific reason you want this to appear when the script is in the project files inspector? If you attach this script to an empty game object it should appear as you want it to... Commented Apr 15, 2020 at 3:47
  • Why not use a ScriptableObject to hold them, if this array should be applied to all script instances? Commented Apr 15, 2020 at 6:18
  • @Jake 1. It inherits from MonoBehaviour, and as I learned that I cannot use it the way I intended, I wanted it to hold some default values for if it doesn't exist in the scene when called. (As I learned that it only works in edit mode) I had to hack together a wierd solution where I created a TileSet : ScriptableObject which I checked in TileManager to see if it was null and call a default from Resources. Commented Apr 19, 2020 at 18:54

2 Answers 2

2

The only field types allowing default references are fields of a type derived from UnityEngine.Object

... a Sprite[] is an array and does not inhertit from UnityEngine.Object.


As mentioned before you could use a ScriptableObject wrapper class such as e.g.

// this attribute adds an entry to the Asset context menu Add->SpriteCotnainer
[CreateAssetMenu]
public class SpriteContainer : ScriptableObject
{
    public Sprite[] sprites;
}

then in your class use a

[SerializeField] private SpriteContainer spriteContainer;

which now is of type ScriptableObject which inherits from UnityEngine.Object so it should show up as default reference field.

So you would need to create this asset via right click on AssetsAddSpriteContainer. Here you can reference all the sprites you need. Then drag this into the spriteContainer field of your script.

Sign up to request clarification or add additional context in comments.

Comments

0

You have two options, either create a gameobject/prefab, add the script to it - and then it will appear.

Or, if it's just a data object you need to access, you can make it a ScriptableObject and create an instance of it.

Either way, you will have to create an instance; you can't assign variables to scripts, only objects with the scripts attached.

1 Comment

This is not entirely true. OP is asking about the default references you can assign on a script/component basis. These default references are then automatically pre-assigned to any newly added component in the Editor. It works only with references of type Object (see my answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.