Can not send data to PHP Server

Hello kind strangers,

I am working on a school project and we need to make a math game. I am very close. There is one problem left that me and another student can’t find the solution for. We need to post the Sum, the Answer, the GameId and the UserId to our PHP server. This will later be used for evaluating. The problem is with posting the sum. We are getting this error:

NullReferenceException UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/MonoBehaviourBindings.gen.cs:62) Answer.WrongAnswer (Int32 answer) (at Assets/Answer.cs:63) MoveCamera.Update () (at Assets/MoveCamera.cs:27)

MoveCamera script:

`public class MoveCamera : MonoBehaviour {

public Vector3 position = new Vector3();

public float Speed = 0.3f;

Answer answer = new Answer();
GenerateSum SumGen = new GenerateSum();
bool wrongAnswer = false;
// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
    transform.position = Vector3.Lerp(transform.position, new Vector3(-110, 93.5f, (float)255.78), Speed * Time.deltaTime);
    if(transform.position.x < -100f)
    {
        if(!wrongAnswer)
        {
            answer.WrongAnswer(0);
            wrongAnswer = true;
        }
    }
    if(transform.position.x < -102f)
    {
        SumGen.Generate();
        wrongAnswer = false;
    }
}

}
`

Answer script code:
`
void OnTriggerEnter(Collider collider)
{
var answer = gameObject.GetComponentInChildren().text;
var sum = GameObject.Find(“SumText”).GetComponent().text;

    List<string> numbers = Regex.Split(sum, @"\D+").ToList();
    int sumAnswer = 0;

    if (sum.Contains("+"))
    {
        sumAnswer = (Convert.ToInt32(numbers.ToArray()[0]) + Convert.ToInt32(numbers.ToArray()[1]));
    }
    else if (sum.Contains("-"))
    {
        sumAnswer = (Convert.ToInt32(numbers.ToArray()[0]) - Convert.ToInt32(numbers.ToArray()[1]));
    }
    else if (sum.Contains("/"))
    {
        sumAnswer = (Convert.ToInt32(numbers.ToArray()[0]) / Convert.ToInt32(numbers.ToArray()[1]));
    }

    if (sumAnswer == Convert.ToInt32(answer))
    {
        CorrectAnswer(Convert.ToInt32(answer));
    }
    else
    {
        WrongAnswer(Convert.ToInt32(answer));
    }

}

public void WrongAnswer(int answer)
{
    Light spot = GameObject.Find("Spotlight").GetComponent<Light>();
    spot.color = new Color(200, 0, 0);
    spot.intensity = 0.009f;
    StartCoroutine(PostSum(answer, 0));
    main.SumCount += 1;
    if(main.SumCount == 10)
    {
        Debug.Break();
        main.SumCount = 0;
        Debug.Log(main.SumCount);
    }
}

public void CorrectAnswer(int answer)
{
    Light spot = GameObject.Find("Spotlight").GetComponent<Light>();
    spot.color = new Color(0, 200, 0);
    spot.intensity = 0.009f;
    StartCoroutine(PostSum(answer, 1));
    main.SumCount += 1;
    if (main.SumCount == 10)
    {
        Debug.Break();
        main.SumCount = 0;
    }
}

public IEnumerator PostSum(int receivedAnswer, int isCorrect)
{
    var sum = GameObject.Find("SumText").GetComponent<TextMesh>().text;
    int userId = Convert.ToInt32(GameObject.FindGameObjectWithTag("Username").name);
    WWWForm form = new WWWForm();
    form.AddField("user", userId);
    form.AddField("gameSum", sum);
    form.AddField("receivedAnswer", receivedAnswer);
    form.AddField("isCorrect", isCorrect.ToString());

    Debug.Log("user: " + userId + " GameSum: " + sum + " receivedAnswer: " + receivedAnswer + " isCorrect: " + isCorrect);

    using (UnityWebRequest www = UnityWebRequest.Post("http://localhost:8000/AnswerInsert.php", form))
    {
        yield return www.Send();
        Debug.Log(System.Text.Encoding.Default.GetString(www.downloadHandler.data));
    }
}

`
It seems like our WrongAnswer function doesn’t get into the Ienumerator.

Anyone who can help me?

Kindest regards

Well that’s easily explained: You created a MonoBehaviour derived class (your Answer class) with the new keyword:

Answer answer = new Answer();

This is not allowed as the resulting instance will not be a valid component since it is not attached to any gameobject in the scene. Therefore you can not call StartCoroutine inside your WrongAnswer method since the Answer instance is not a value component. You probably either want to attach the Answer component to one of your gameobjects manually, make the answer variable public and assign the instance in the inspector, or you create the instance dynamically in Start or Awake using

answer = gameObject.AddComponent<Answer>();