1
\$\begingroup\$

I made a Unity Prefab where about 5 things vary. I then made an invisible GameObject where I use Visual Scripting to hardwire the 5 differing items, in order to create the various GameObjects I need.

It is hardwired, but the Prefab is working really well for my purposes. Here's a sample part of that script, where I'm setting the color of some text and also the sprite.

enter image description here

I'd like to make the whole thing less hardwired, and to keep track of these 5 things in a database of sorts. So I defined a Scriptable Object with the 5 variables in it:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New NeemData", menuName = "Neem Data", order = 51)]

public class NeemData : ScriptableObject
{
    [SerializeField]
    private string neemName;

    [SerializeField]
    private Vector3 neemPosition;

    [SerializeField]
    private Sprite singleNeemSprite;

    [SerializeField]
    private string feemText;

    [SerializeField]
    private Color feemColor;

}

I've been able to easily create Scriptable Objects with data in it, like this one:

enter image description here

I'd like to be able to populate my Prefab with the data from that Scriptable Object, and create GameObjects with that workflow. But I'm not sure the best way to get data out of a Scriptable Object, and combined with a Prefab template to make GameObjects. Any advice?

\$\endgroup\$
9
  • \$\begingroup\$ Your prefab can have just a variable of your scriptable object private NeemData myDataHolder. For code references, please use the code markup instead of screenshots \$\endgroup\$
    – Zibelas
    Jun 6, 2022 at 16:05
  • \$\begingroup\$ @Zibelas I'm using Visual Scripting... what sort of code markup would be best? \$\endgroup\$
    – kanamekun
    Jun 6, 2022 at 16:10
  • \$\begingroup\$ Copy paste your code (visual script + screenshot is fine, but NeemData is a script) and just click the script markup in the text field. It should make 4 spaces in front, you might need to add them to the first line and you get a wonderful code format \$\endgroup\$
    – Zibelas
    Jun 6, 2022 at 16:15
  • \$\begingroup\$ And keep in mind, scriptable objects are the same for all instances. Your position vector on them will be slightly problematic if you ever want to spawn two cats with that property at different positions \$\endgroup\$
    – Zibelas
    Jun 6, 2022 at 16:18
  • 1
    \$\begingroup\$ Ah I see now, I should not have set my Scriptable Objects variable to be private... \$\endgroup\$
    – kanamekun
    Jun 6, 2022 at 18:32

1 Answer 1

1
\$\begingroup\$

I changed the Scriptable Object variables to be public, and everything worked perfectly.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New NeemData", menuName = "Neem Data", order = 51)]
public class NeemData : ScriptableObject
{
    [SerializeField]
    public string neemName;

    [SerializeField]
    public Vector3 neemPosition;

    [SerializeField]
    public Sprite singleNeemSprite;

    [SerializeField]
    public string feemText;

    [SerializeField]
    public Color feemColor;
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .