How to build a Project with AssetDatabase

Hello guys,
i´m new at Unity and i got a Problem.
I have a database with information and i want to Create Scriptable Objects at runtime. When i Export that project i got an error: " Unity Editor is not in the current context" to fix the Export i use #if UnityEditor. The export is an Webgl project and i want that the export is creating the Scriptable Object and create object´s in the scene.

Is there another way except Asset database to create Scriptable Objects and load Scriptable Objects from an folder??

Code:
#if UNITY_EDITOR
UnityEditor.AssetDatabase.CreateAsset(so, “Assets/Resources/” + this.name + “_” + z + “.asset”);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
if (clear == true)
{
z–;
clear = false;
}
#endif

What are the scriptable objects currently used for?

@HueSamai I save my Data from my Database into the Scribtable objects to get the Information tomy Prefabs.
I try it before without the Scriptable Objects,but then he duplicate some information.

Can you not use a class that saves the information and then sends it to a prefab? For instance, instead of adding a new asset you just create a new variable, like ScriptableObjectName variableName = new ScriptableObjectName();

Of course you would have to replace the ScriptableObjectName with the class name of the scriptable object.

You could then add information to the variable to store.

In the Awake Methode i create an inctance of this object.
This is the full code from the part where i create the Scribtable object.

In an other Script i add the Scriptable Object to an Prefab.

Code:

{
if(z<anzahl)
{

var so = ScriptableObject.CreateInstance();
// Debug.Log(beschreibung[zahl]);
if (create == false)
{
so.ID = getID[z];
so.nameText = beschreibung[z];
so.BildImage = url[z];
so.X = posX[z];
so.Z = posZ[z];

#if UNITY_EDITOR
UnityEditor.AssetDatabase.CreateAsset(so, “Assets/Resources/” + this.name + “_” + z + “.asset”);
UnityEditor.AssetDatabase.SaveAssets();
UnityEditor.AssetDatabase.Refresh();
if (clear == true)
{
z–;
clear = false;
}
#endif
}
else
{
// transform.position += new Vector3(posX[z], 0, posZ[z]);
var temp = Instantiate(prefab, transform.position, Quaternion.identity);
string oName = this.name + “_” + z;
pfad = oName + “.asset”;
temp.name = oName;
temp.tag = “Respawn”;
// transform.Parent(this.transform);
temp.transform.SetParent(this.transform);
}
z++;
}
}

Code Where i load the Scriptable object and save the new Prefab :

#if UNITY_EDITOR
info = AssetDatabase.LoadAssetAtPath(x);
#endif
nameText.text = info.nameText;
X = info.X;
Z = info.Z;
// transform.LookAt(transform.parent);
StartCoroutine(GetImage());
#if UNITY_EDITOR
transform.position += new Vector3(X, 0, Z);
string localPath = “Assets/Resources/Prefab/” + gameObject.name + “.prefab”;
PrefabUtility.SaveAsPrefabAssetAndConnect(gameObject, localPath, InteractionMode.UserAction);
#endif

I want to have an Export WebGl which is creating the Scriptable Objects( or somethink where i can do the same) an set an Prefab into the sceen

this 2 emty object got the same script and when i do it like this:

ScriptableObjectName variableName = new ScriptableObjectName();

the first Script will refresh the information before i can transfer it into an object or i am wrong?

6578326--746764--bild.PNG

Instead of creating the scriptable object and storing the values, then loading the values and assigning them to a prefab, can’t you just load the information and then set the values. On the script where you load the scriptable object data (I assume this is for the prefab), have a global public variable called X and Z. Then instead of saying creating a scriptable object, just assign the values. Here is an example.

var temp = Instantiate(prefab, transform.position, Quaternion.identity);

string oName = this.name + "_" + z;

pfad = oName + ".asset";
temp.name = oName;
temp.tag = "Respawn";
// transform.Parent(this.transform);
temp.transform.SetParent(this.transform);
temp.GetComponent<ScriptName>().X = posX[z];
temp.GetComponent<ScriptName>().Z = posZ[z];

then in the script for prefabs:

//Declare variables
var X;
var Z;

...

// transform.LookAt(transform.parent);
StartCoroutine(GetImage());

transform.position += new Vector3(X, 0, Z);

Hopefully this helps. This is not intended to be copied just to give you an idea of what to do. Also I would like to point out, that you are using UnityEditor.AssetDatabase and PrefabUtility, as they were not intended to be used. That is why I removed the saving Prefab bit of the code. Why do you want to save the prefabs, so that I can help you find an alternative.

1 Like

The information will be lost, if it is not a global variable. So if it is created in a function, it will be created everytime that function is run, making the information not carry over. But if it is a global variable than it will not.

@HueSamai Tank you very much that is working now :smile:

That’s great to hear! :slight_smile: