Buttons to Scriptable objects

Hello!
I am trying to use Scriptable Objects but there’s a problem I can not solve. Each object has a button and some different int variable x defined by a developer. I also have a non related counter.

So I have for example 3 objects:

  • Object 1 - x = 1,
  • Object 2 - x = 2,
  • Object 3 - x = 3,
  • And a counter = 0

If I click a button of object 1, I want my counter to add 1, if object 3 - 3. I really want it to do by Scriptable Objects.

How exactly should I do this?

For anyone who might be looking for the answer now:


You need to write a custom editor script and its super easy, create the below 2 scripts :

  • TestScriptable is your scriptable object script
  • TestScriptableEditor is your editor script that will create a button in your scriptable object asset(you can place this script under Editot Folder)

TestScriptable.cs

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "SO", menuName = "Create SO/SO")]
public class TestScriptable: ScriptableObject
{
    public int value;

    public void UpdateCounter()
    {
        counter += value; // make sure you assign the reference to counter variable
    }
}

TestScriptableEditor.cs

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

[CustomEditor(typeof(TestScriptable))]
public class TestScriptableEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        var script = (TestScriptable)target;

            if(GUILayout.Button("Add to Counter", GUILayout.Height(40)))
            {
                script.UpdateCounter();
            }
        
    }
}

Now create as many Assets from the ‘TestScriptable’ and it will have a Button in it that calls the ‘UpdateCounter’ method.

You won’t need scriptable objects to do this it would be better to have the counter as a static int. Then in the sfunction being called by the button would all be accessing the same counter reference variable.

I had this script for displaying cars thanks to scriptable objects, hope it can help:

public class CreateButtons : MonoBehaviour
{

    [SerializeField] Button prefabButton;
    [SerializeField] GameObject buttonPanel;
    [SerializeField] Cars[] cars;
    [SerializeField] TMP_Text textModel;
    [SerializeField] TMP_Text textDescription;
    [SerializeField] TMP_Text textPrice;

    GameObject carInScene;


    void Start()
    {
        foreach (Cars car in cars)
        {
            Button newButton = Instantiate(prefabButton, buttonPanel.transform);
            newButton.image.sprite = car.icon;
            newButton.onClick.AddListener(() => IstantiateCar(car));
        }
    }    public void InstantiateCar(Cars car)
    {
        if (carInScene!= null)
        {
            Destroy(carInScene);
        }
        carInScene = Instantiate(car.prefab, new Vector3(0, 0, 0), Quaternion.identity);
        textModel.text = car.model;
        textDescription.text=car.description;
        textPrice.text = car.price.ToString() + "€";
    }

[CreateAssetMenu(menuName = "Cars")]
public class Cars : ScriptableObject
{
    public string model;
    public string description;
    public int price;
    public GameObject prefab;
    public Sprite icon;
    
}