My Scriptable Object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (fileName = "Hero", menuName = "Create HeroValuesAsset")]
public class HeroValues : ScriptableObject
{
ListsScript listsScript = new ListsScript();
[SerializeField]
private int heroHP;
[SerializeField]
private string heroName;
public int HeroHP
{
get
{
return heroHP;
}
}
public string HeroName
{
get
{
return heroName;
}
}
public void PassValue()
{
int heroHP1 = HeroHP;
listsScript.RecieveValue(heroHP1);
}
}
My ListsScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ListsScript
{
public int listInt1;
public void RecieveValue (int heroHP1)
{
heroHP1 = listInt1;
Debug.Log(listInt1 + " is the value of listInt1 in the RecieveValue method.");
}
public void TestValue()
{
Debug.Log(listInt1 + " is the current value of listInt1 instance variable.");
}
}
My TestScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
ListsScript listsScript = new ListsScript();
void Start()
{
}
void Update()
{
listsScript.TestValue();
}
}
What is happening in my script:
I have made several ScriptableObject assets, each storing two individual values: heroHP and heroName. Those ScriptableObjects are assigned each to their own UI Buttons. The buttons are set to call the TestValue() OnClick in the inspector, with the object being the ScriptableObject asset (1 per button). OnClick() the button with the “assigned” SO passes the value of heroHP to the RecieveValue(heroHP1) in the ListsScript. Then, it assigns the value of heroHP1 to listInt1 instance variable.
On play mode, the button presses are registering the data passing to the method and i recieve the console message with the value and " is the value of listInt1 in the RecieveValue method." attached. Wonderful. However, the constantly updating listsScript.TestValue() call in the Update() of the TestScript MonoBehaviour remains at zero.
What am i trying to achieve:
I want the value of listInt1 of the ListsScript to change in accordance with whatever according to whatever value PassValue() passes.
Why am i trying to do this:
To use the “decoupled” value to add it to a List as adding the value currently as it is, results in each SO having their own individual list.
What have i already tried to solve this issue:
- the solution above (directly changing the value of the field).
- adding a getter and using RecieveValue(int heroHP1) as a setter, while making the listInt1 private and swapping the listInt1 in the TestValue() for the method (TestValue() still showing up zero).
- using a constructor:
[code=CSharp] using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ListsScript
{
public class ListIntValue
{
public int listInt1{ get; set; }
public ListIntValue(int valueA)
{
listInt1 = valueA;
}
}
public void RecieveValue(int heroHP1)
{
var heroHPvar = new ListsScript(heroHP1);
TestValue(heroHPvar);
Debug.Log(heroHPvar + " is the value of listInt1 in the RecieveValue method.");
}
public void TestValue(ListsScript.ListIntValue heroHPvar)
{
Debug.Log(heroHPvar + " is the current value of listInt1 instance variable.");
}
}
which results into “ListsScript + ListIntValue is the current value of listInt1 instance variable.” shown in console on click with the TestScript MonoBehaviour disabled.