Constructors and MonoBehaviour - using update()

Hi guys,
I need your help.

I have two class: class TrafficSignalsManager : MonoBehaviour and TrafficSignalsSpawner : MonoBehaviour
I want to spawn all my traffic lights and after this I want to send my intersersections (every intersection has traffic lights) to TrafficSignalsManager and to create a functionality for every traffic lights.

The problem is: when I press the play button, my intersections variable is allways null.


Can you help me, please?

It’s not possible to instantiate MonoBehaviours using constructors. Unity’s console should’ve notified you about that already.

If you really want to instantiate it, you should create a new gameobject, and attach the component to it.

GameObject obj = new GameObject("SomeName);
YourComponent comp = obj.AddComponent<YourComponent>();
comp.SetIntersections(something);

Better approach is to have a prefab pre-made, with that manager attached to it.
That way you could use Instantiate(…) and GetComponent to obtain the reference from it.

Note that there’s a different (design) approach to what you’re doing right now.
I’ll just leave it be. There’s no fun in learning, when there’s no learning involved.

Yeah, but I want to send a list with GameObjects