How should initialisation be handled in classes that inherit from monobehaviour scritps?

using System.Collections;

public class ClassA : Class B 
	public bool varB;//how is this initialised?

using UnityEngine;
using System.Collections;

public class ClassB : MonoBehaviour 
	public bool varA;

    void Start()
		varA = true;

How is varB initialised?

Hi, You basically inherit a monobehaviour, so to initialize that variable, you can have following methods:

  1. You can attach ClassA to a gameobject in the scene. It will automatically initialized.
  2. You can directly call an object of ClassA and initialize the variable
    ( I guess you would have known this already )

However, there can be different scenarios:

  1. When ClassA is attached to a GameObject and ClassB not:
    The Start() of ClassB is called automatically as there is no override function in derived class. So, varA and varB, both will be initialized. (You can see both varA and varB in inspector and you can initialize from there also).

  2. When ClassB is attached and classA is not:
    The Start() of ClassB is called but no effect in ClassA so you need to call Object of ClassA in ClassB as:
    ClassA a;
    a.varB = false;//Initialization
    where ever you want. In order to call start of ClassA, you need to call it explicitly.

  3. If no script is attached:
    You can call the object of ClassA from other script and initialize it explictly.