- Home /
My scripts are not working properly
So my scripts come in three parts 1st part is Keybind 2nd part is KeyController and finally the 3rd part is function i want done if key is pressed. But issue is KeyController isnt pulling Keybind scripts unless object in the inspector is selected during play mode. If i pause it then select Object that has scripts on it after i resume it magically works. Its important to note if Main Object that has codes is selected then scene is played it still has these errors
Errors
NullReferenceException: Object reference not set to an instance of an object
KeyController.Update () (at Assets/Settings/KeyController.cs:38)
NullReferenceException: Object reference not set to an instance of an object
Option.Update () (at Assets/Settings/Option.cs:39)
NullReferenceException: Object reference not set to an instance of an object
EquipDisplay.Update () (at Assets/servertest/Scripts/EquipDisplay.cs:23)
Keybind
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using UnityEngine.EventSystems;
public class KeybindV2 : MonoBehaviour {
public MyKeys AllKeys;
[System.Serializable]
public class MyKeys
{
//public GameObject KeyOBJ;
public string keyname;
public KeyCode key;
public bool changekey;
public Text Disc;
public Button KeyButton;
public Text KeyTxt;
}
// Use this for initialization
void Start()
{
AllKeys.keyname = AllKeys.Disc.gameObject.name;
AllKeys.Disc.text = AllKeys.keyname;
AllKeys.key = (KeyCode)System.Enum.Parse(typeof(KeyCode), PlayerPrefs.GetString(AllKeys.keyname, AllKeys.key.ToString()));
AllKeys.KeyButton = AllKeys.Disc.gameObject.transform.GetChild(0).GetComponent<Button>();
AllKeys.KeyTxt = AllKeys.KeyButton.gameObject.transform.GetChild(0).GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
AllKeys.KeyTxt.text = AllKeys.key.ToString();
AllKeys.KeyButton.onClick.AddListener(delegate () { this.ChangeKey(); });
if (AllKeys.changekey)
{
AllKeys.KeyTxt.text = "Unassigned";
foreach (KeyCode kcode in System.Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKeyDown(kcode))
{
AllKeys.key = (KeyCode)System.Enum.Parse(typeof(KeyCode), kcode.ToString());
AllKeys.changekey = false;
}
}
}
}
public void ChangeKey()
{
AllKeys.changekey = true;
}
public void SaveKey()
{
PlayerPrefs.SetString(AllKeys.keyname, AllKeys.key.ToString());
}
}
KeyController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
public class KeyController : MonoBehaviour {
[System.Serializable]
public class KeyData
{
public KeyCode Key;
public string KeyName;
}
[HideInInspector]
public bool save;
[HideInInspector]
public KeyData[] KeyINFO;
[HideInInspector]
public KeybindV2[] KeyCS;
[HideInInspector]
public bool MenuLock;
// Use this for initialization
void Awake () {
KeyCS = this.gameObject.GetComponents<KeybindV2>();
KeyINFO = new KeyData[KeyCS.Length];
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < KeyINFO.Length; i++)
{
KeyINFO[i].Key = KeyCS[i].AllKeys.key;
KeyINFO[i].KeyName = KeyCS[i].AllKeys.keyname;
}
}
public void SaveKeys()
{
for (int i = 0; i < KeyINFO.Length; i++)
{
PlayerPrefs.SetString(KeyINFO[i].KeyName, KeyINFO[i].Key.ToString());
}
}
}
One functions
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipDisplay : MonoBehaviour {
public GameObject InvenOBJ;
public KeyController KeyCS;
public bool showinven;
// Use this for initialization
void Start () {
KeyCS = this.gameObject.GetComponent<KeyController>();
}
// Update is called once per frame
void Update()
{
InvenOBJ.SetActive(showinven);
for (int i = 0; i < KeyCS.KeyCS.Length; i++)
{
if (KeyCS.KeyINFO[i].KeyName == "Inventory" && !KeyCS.MenuLock)
{
if (Input.GetKeyDown(KeyCS.KeyINFO[i].Key))
{
showinven = !showinven;
}
}
else
{
break;
}
}
}
}
quick comment to add is that errors occur twice if object that has scripts applied to it is selected in the inspector when play mode is pressed and then all the scripts are fully functional one again I have tried doing if else return method as well as if null else neither of these have worked