0

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
  • 1
    You can totally do so (ScriptableObject.CreateInstance<YourType>() or Instantiate(existingScriptableObject)) .. mostly it makes no sense though. ScriptableObject is 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. Commented Feb 15, 2022 at 20:06

1 Answer 1

0

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.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.