Configurable values for a prefab/template object. How should I do it?

(This is my first forum post and I’m terribly sorry if this thread happens to be inappropriate. I assure you I did my best to search for similar questions)

I’m making a game/physics simulation toy. The main subject here is the user’s ability to create individual moving objects by pressing UI buttons. The way I visualize it is having a sort of “form” window in which you set the object’s speed, direction, mass etc. and an object will be created based on your settings, from an “empty” prefab. However, I am very lost about how to do the logistics here.

So far in my project I have placeholder code to make a little car move whenever it knows the scene isn’t “paused”. I figured maybe it would be best to have different scripts for handling the object’s interaction with the world, and to set its values. Would that be redundant? If not, what is a good way to make it happen?
More specifically, how would the value attribution go here? Should I have a script with placeholder values and methods for setting them, then have the “active behavior” script refer to them?

I’m working on Unity 2D and using rigidbody2d to handle the movement.

(I’m not including code because I don’t feel like I did enough so far that it would be relevant, or that I would want to keep it)

You could look into ScriptableObjects as a way to provide predefined settings (that you make) for all the parameters.

You could also let the user create their own (transiently) and use those the same way you use your predefined ones.

That way your “main engine” thingy only knows how to suck the values out of that type of ScriptableObject and use them appropriately.

This is how I do almost everything that smells like this in my games. For instance, here is the gravity configuration ScriptableObject for my Jetpack Kurt game:

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

[CreateAssetMenu(menuName = "SpaceFlight/SpaceFlightGravitySetting")]
public class SpaceFlightGravitySetting : ScriptableObject
{
    public    string    Description;

    public    float    GravityMagnitude;

    public    bool    UsableForTutorial;

    void Reset()
    {
        Description = "NONE";

        GravityMagnitude = 1.00f;
    }

    public override string ToString ()
    {
        return string.Format ("[SpaceFlightGravitySetting:{0}:{1}g]", Description, GravityMagnitude);
    }

    public string ToStringShort()
    {
        return string.Format ("[{0}:{1}g]", Description, GravityMagnitude);
    }

    string FormatGravity( float magnitude)
    {
        return System.String.Format( "{0:0.0}m/s^2", magnitude);
    }

    [System.NonSerialized] string NumericDescription;

    public string GetNumericDescription()
    {
        if (NumericDescription == null)
        {
            NumericDescription = System.String.Format( FormatGravity( GravityMagnitude));
        }

        return NumericDescription;
    }
}

Then I have as many of those as I want, each one a file on the disk that I can slot in when active. Those are all predefined, but if I wanted, I could make an editor that lets the user tinker with individual values all they want.

1 Like

Thank you very much!