There is a way to store a float array in a prefab?

I would like to store float array in the hard disc in an easy way. I though I could store this data in a prefab like others common variables does, but it doesn’t.

How can I do this?

I’m trying to do this because I’m creating terrains with random 3D textures using Perlin Noise. I would like to keep some of those textures when terrains looks good.

I know I can store data using serializable classes, that will be my option if I cant use prefabs.

Thanks in advance !

I’m not quite sure what you mean, but using a standard float[ ] array (or a List) serializes just fine.

–Eric

You CAN do this, but it’s a bit fiddly and I would not recommend it.

You can do it by making a script that contains a public array of float. Put that script on a blank game object and then run the editor and fill the array with data again and again until you see the terrain you like.

At that point (with the editor and game still running!!) you would drag that gameobject from the Hierarchy into the Project folder to make a prefab, which would have those values in it.

However, that is a wee bit awkward, as well as big fragile, like if you forget and press STOP on the player.

I would recommend you look into serializing a class containing just the data you want, then deserializing it on demand. There are plenty of tutorials out there about how this can be done.

The thing you are looking for is a ScriptableObject. It’s great for storing and configuring data within the Unity system.

1 Like

Hi !

I have tried using ScriptableObject to store the array, but it doesn’t seems to work.

This is my object for storing the data.

[Serializable]
public class PersistentSample3D : ScriptableObject
{
    public float[,,] sample;
    public int seted = 0;

    public void setSample(float[,,] _sample){
        Debug.Log("sample at 000: "+_sample[0,0,0]); //this also verifies that the data is not null
        this.sample = _sample;
        seted = 1;
    }
}

This is how i create the asset:

private void createPerlinAsset(){

        PersistentSample3D sample3D = ScriptableObject.CreateInstance<PersistentSample3D>();
        sample3D.setSample (NoiseGenerator.Perlin (octave)); // <-- This is the data i want to store
        AssetDatabase.CreateAsset (sample3D, "Assets/Resources/Prefab/Sample3D/"+name+".asset");
        AssetDatabase.SaveAssets ();
    }

Finally i have a component that uses the new asset for test if the data was stored sucessfully:

public class PrefabTest : MonoBehaviour
{
    public PersistentSample3D data;

    void Start ()
    {
        Debug.Log( "was seted: "+data.seted);
        Debug.Log( "in 000: "+data.getSample()[0, 0, 0]);
    }
}

The first log is “was seted: 1”, that tell us that the data was seted properly, and before the second log there is a null reference exception, therefore, the data is not stored (but the seted variable does).

Could you help me to find the problem? I think im doing in correctly :c.
Thanks !