i think this has something to do with Serializable but i don’t know enough to say how to fix this.
here is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ControlPanel : MonoBehaviour
{
/*
*
*
* c l a s s e s
*
*
*/
public abstract class BoolControl {
protected string playerprefName;
public Text buttonText;
public Button btn;
public bool defaultState;
protected bool state {
get
{
return state;
}
set {
if(value){
buttonText.text = "OFF";
ColorBlock cb = btn.colors;
cb.normalColor = Color.blue;
btn.colors = cb;
}else{
buttonText.text = "ON";
ColorBlock cb = btn.colors;
cb.normalColor = Color.black;
btn.colors = cb;
}
state = value;
}
}
public BoolControl () {
btn.onClick.AddListener(()=>ToggleState());
Debug.LogError("added event listener");
}
public void ToggleState()
{
Debug.LogError("State change");
state = !state;
ApplyChanges();
}
public abstract void ApplyChanges();
public void SavePlayerpref()
{
PlayerPrefs.SetInt(playerprefName, System.Convert.ToInt32(state));
}
public void ReadPlayerpref()
{
state = System.Convert.ToBoolean(PlayerPrefs.GetInt(playerprefName, System.Convert.ToInt32(defaultState)));
}
}
[System.Serializable]
public class Syphon : BoolControl
{
public syphonController script;
public Syphon():base(){
playerprefName = "SyphonState";
}
public override void ApplyChanges(){
}
}
[System.Serializable]
public class Hue : BoolControl {
public HueLightController script;
public Hue():base(){
playerprefName = "HueState";
}
public override void ApplyChanges()
{
script.on = state;
if(state){
}else{
}
}
}
[System.Serializable]
public class Resolution {
string[] playerprefName = { "resolutionx", "resolutiony" };
public SuperWideScreen SWC;
public Text txtAspectRatioX;
public Text txtAspectRatioY;
public Text txtCurrentRes;
public Button btnConfirm;
Vector2 res;
public Vector2 defaultRes;
public void ApplyChanges(){
//SWC.SetSize(res);
}
public void SavePlayerpref(){
PlayerPrefs.SetFloat(playerprefName[0], res.x);
PlayerPrefs.SetFloat(playerprefName[1], res.y);
}
public void ReadPlayerpref(){
res.x = PlayerPrefs.GetFloat(playerprefName[0], defaultRes.x);
res.y = PlayerPrefs.GetFloat(playerprefName[1], defaultRes.y);
}
}
/*
*
*
*
*
* m e t h o d s
*
*
*
*/
public Resolution resolutionControl;
public Syphon syphonControl;
public Hue hueControl;
private void SaveStates() {
}
public void UpdateStates(){
SaveStates();
}
private void ReadStates() {
resolutionControl.ReadPlayerpref();
syphonControl.ReadPlayerpref();
hueControl.ReadPlayerpref();
}
private void setColors(){
}
//void Awake () {
//}
// Use this for initialization
void Start () {
resolutionControl = new Resolution();
hueControl = new Hue();
syphonControl = new Syphon();
ReadStates();
resolutionControl.ApplyChanges();
syphonControl.ApplyChanges();
hueControl.ApplyChanges();
}
// Update is called once per frame
void Update () {
}
}
I have linked the Button to the field Btn
,( see pic attached) but when I run the program, it shows me that it can’t find btn.
NullReferenceException: Object
reference not set to an instance of an
object ControlPanel+BoolControl…ctor
() (at
Assets/ControlPanel/ControlPanel.cs:46)