Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Bart_Stitch · Jul 02, 2019 at 02:58 PM · wwwweb

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 02, 2019 at 05:04 PM

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>();
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

111 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

scraping from a web page with minimal use? 0 Answers

Having Trouble with Post to Server 0 Answers

AddBinaryField always missing bytes in UnityWebRequest Post on iOS 0 Answers

HTTP Delete calls are possible? 0 Answers

How to load HTML page inside unity game? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges