I am trying to make an app where the user can create a new player and enter some data about its sports performances. To do that, I created a Scriptabe Object Class "Players" with (for the moment) only a name. I would like to be able to create/instantiate a new player when clicking on a "Add new Player" Button. How can I create a new ScriptableObject in Game and define its name?
1 Answer
You can create a simple void that gets called whenever you click a button. Something like this:
public void AddNewPlayer() {
new Player();
}
Because you want to give the player a name you can just pass the name in as a parameter.
Your Player Object could look like this:
class Player {
String name;
public Player(String name) {
this.name = name;
}
}
If you want the player to put in their own name you can do this with an input field.
And then in the AddNewPlayer function you just read the input of the inputField and use this as the name.
ScriptableObject.CreateInstance<YourType>()orInstantiate(existingScriptableObject)) .. mostly it makes no sense though.ScriptableObjectis an asset in the Unity Editor. Innyoir later built game there is no way to save such an asset anyway => it can as well be a "normal" serializable class that you store and load somewhere via JSON e.g.