NullReferenceException from calling a method from another class.

Hello,
I am new to programing and have been trying to learn how to use multiply scripts instead of just using one. I have ran into a problem with the following code. When I call the getNextTriangle method from the MasterGame script I get this error.

NullReferenceException: Object reference not set to an instance of an object
CreateTriangles.getNextTriangle (Int32 xSide) (at Assets/C#/CreateTriangles.cs:24)
MasterGame.Start () (at Assets/C#/MasterGame.cs:13)

public class CreateTriangles : MonoBehaviour {
    public GameObject trianglePreFab;
    private List<GameObject> triangles;
    private Rigidbody2D rb;

    void Start () {
        triangles = new List<GameObject>();
        for(int i = 0; i < 5; i++)
        {
            triangles.Add(Instantiate(trianglePreFab, new Vector3(0, -10, 0),trianglePreFab.transform.rotation));
            triangles*.gameObject.SetActive(false);*

}
}
/I can call the getNextTriangle method from within this script and it works with no problems. However when I call it from my other script, its like triangles is empty, it never gets set up from Start./
public void getNextTriangle(int xSide)
{
for (int i = 0; i < triangles.Count;i++) //This is where the error starts.
{
if (!triangles*.activeInHierarchy)*
{
triangles*.transform.position = new Vector3(xSide, -5, 0);*
triangles*.SetActive(true);*
rb = triangles*.GetComponent();*
rb.AddForce(transform.up * 500);
break;
}
else
{
triangles.Add(Instantiate(trianglePreFab, new Vector3(xSide, -5, 0),trianglePreFab.transform.rotation));
}
}
}
And here is the other script I am trying to call it from.
public class MasterGame : MonoBehaviour {
private CreateTriangles triangle;
void Start()
{
triangle = GameObject.Find(“This is the game object CreateTriangles is on”).GetComponent();
triangle.getNextTriangle(-2);
}
Any help would be greatly appreciated.
Thanks

It is because your Start() method from MasterGame runs before the Start() method of CreateTriangles script. A few ways to overcome this is:

  1. You can change the script execution order to execute CreateTriangles script to run before MasterGame script from Script Execution Order Settings
  2. You can move your code from Start() method of CreateTriangles to Awake() method as Awake() is called before any Start() methods. You can read more about Execution Order of Event Functions for further details.