I am trying to use nested classes and call functions in the base class of the script.
public class componentEnable : MonoBehaviour
{
private DisableMeshOnStart disableMeshOnStart_ = new DisableMeshOnStart();
private EnableCanvasOnStart enableCanvasOnStart_ = new EnableCanvasOnStart();
public DisableMeshOnStart[] disableMeshOnStartGroup;
public EnableCanvasOnStart[] enableCanvasOnStartGroup;
void Start()
{
disableMeshOnStart_.OnStart();
enableCanvasOnStart_.OnStart();
}
// Update is called once per frame
void Update()
{
}
[Serializable]
public class DisableMeshOnStart
{
public string groupTitle;
public bool disableMeshOnStart_ = false;
public GameObject[] disableMeshes;
public void OnStart()
{
int howMany = disableMeshes.Length;
Debug.Log(howMany);
foreach (GameObject asset in disableMeshes)
{
if (disableMeshOnStart_ == true)
{
asset.GetComponent<MeshRenderer>().enabled = !true;
}
}
}
}
[Serializable]
public class EnableCanvasOnStart
{
public string groupTitle;
public bool enableCanvasOnStart_ = false;
public GameObject canvasUI;
public void OnStart()
{
Debug.Log("hello");
if (enableCanvasOnStart_ == true)
{
Debug.Log("beep");
canvasUI.GetComponent<Canvas>().enabled = true;
}
}
}
}
There errors I get are:
NullReferenceException: Object reference not set to an instance of an object
componentEnable+DisableMeshOnStart.OnStart () (at Assets/Scripts/V2/componentEnable.cs:41)
componentEnable.Start () (at Assets/Scripts/V2/componentEnable.cs:19)
I am trying to understand why I cant call these functions properly. The game objects are assigned properly in the inspector.
If placed in a separate class, like so than it does work as expected.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public string groupTitle;
public bool disableMeshOnStart_ = false;
public GameObject[] disableMeshes;
public void Start()
{
OnStart();
}
public void OnStart()
{
int howMany = disableMeshes.Length;
Debug.Log(howMany);
Debug.Log("Forloop");
foreach (GameObject asset in disableMeshes)
{
if (disableMeshOnStart_ == true)
{
asset.GetComponent<MeshRenderer>().enabled = !true;
}
}
}
}
This script, with the help of answers in the thread, work as expected, but I would still like to understand how to create arrays of the classes:
public class ComponentEnable : MonoBehaviour
{
[SerializeField]
private EnableCanvasOnStart enableCanvasOnStart_ = new EnableCanvasOnStart();
[SerializeField]
private DisableMeshOnStart disableMeshOnStart_ = new DisableMeshOnStart();
//private DisableMeshOnStart[] disableMeshOnStartGroup;
//private EnableCanvasOnStart[] enableCanvasOnStartGroup;
void Start()
{
disableMeshOnStart_.OnStart();
enableCanvasOnStart_.OnStart();
}
[System.Serializable]
public class EnableCanvasOnStart
{
public string groupTitle;
public bool enableCanvasOnStart_;
public GameObject canvasUI;
public void OnStart()
{
Debug.Log("2");
if (enableCanvasOnStart_ == true)
{
Debug.Log("beep");
canvasUI.GetComponent<Canvas>().enabled = true;
}
}
}
}
[System.Serializable]
public class DisableMeshOnStart
{
public string groupTitle;
public bool disableMeshOnStart_;
public GameObject[] disableMeshes = new GameObject[0];
public void OnStart()
{
Debug.Log("1");
int howMany = disableMeshes.Length;
foreach (GameObject asset in disableMeshes)
{
if (disableMeshOnStart_ == true)
{
asset.GetComponent<MeshRenderer>().enabled = !true;
}
}
}
}
NullReferenceException
means that you're calling a method on anull
reference. I think that eitherdisableMeshes
isnull
, or at least one of them doesn't have aMeshRenderer
. How about putting some guard conditionals, or trace through the code? \$\endgroup\$ – Panda Pajama Nov 5 '20 at 2:46DisableMeshOnStart.OnStart()
method. In fact, I think that this is the line that causes the problem:int howMany = disableMeshes.Length;
- yourdisableMeshes
is never initialized, so it's null, and you can't callLength
on a null. \$\endgroup\$ – Filip Milovanović Nov 5 '20 at 3:54