I’ve created a custom class but for some reason it throws a null exception when I try to run the code that was otherwise fine. The problem is on the line with _renderer[clone] statement.
private Machine[] _machineClone;
void Start()
{
// Set machines and properties array
_machineClone = new Machine[machineCount];
...
// Spawn machine
_machineClone[count] = Instantiate(machine, pos, Quaternion.identity) as Machine;
// Get machine renderer to use later
_renderer[count] = _machineClone[count].gameObject.GetComponent<Renderer>() as Renderer;
...
public class Machine : MonoBehaviour
{
public GameObject gameObject;
}
I’m guessing it is throwing the exception on the line with the GetComponent()? It is hard to tell what might be causing it with the limited look at the code, since I’m not sure how things are instantiated, but could it be an off by one error with count vs machineCount? _machineClone would be indexed from 0 up to machineCount - 1, but maybe your count is going all the way up to machineCount?
Instantiatereturns a reference to the GameObject just instantiated, but you are casting it to your Machine script, which is not a GameObject and probably causes the null value you are observing.