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: Object1 - x=1, Object2 - x=2, Object3 - x=3, And a counter=0 If I click a button of object 1, i want my counter to add 1, if object3 - 3. I really want it to do by scriptable objects. How exactly should I do this?
Answer by jinincarnate · Mar 19, 2020 at 03:57 PM
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.
Answer by cardcastleusa · Aug 14, 2018 at 05:26 PM
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.
Your answer
Follow this Question
Related Questions
Dymanic buttons with different openURL links 0 Answers
Abillity system for turn based strategy 0 Answers
Button make object teleport (simple) 1 Answer
Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers
Serialization issue after updating Unity from 2018.2.1 to 2019.2.6 1 Answer