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.
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.