Below is a complete code. There the object quoteWS is instantiated to get access to all web service methods.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Main : MonoBehaviour {
public Text quizNumber;
public Text quizText;
public Text feedbackText;
public Button option1;
public Button option2;
// Use this for initialization
void Start () {
Debug.Log (“Start web service FROM MAIN METHOD”);
quoteWS quiz = new quoteWS ();
//int quizId = quizNumber.text;
string question=quiz.showRandomQuiz().Replace(“[”,“”).Replace(“]”,“”);
string[ ] breakStringToArray = question.Split (‘,’);//split result string into an array
string answerOption1 = breakStringToArray [2];//set string for the first option
option1.GetComponentInChildren ().text = answerOption1;//set the button text to the answer option number 1
string answerOption2 = breakStringToArray [4];//set string for the first option
option2.GetComponentInChildren ().text = answerOption2;//set the button text to the answer option number 1
//reset text button colors and feedback text field
option1.GetComponent<UnityEngine.UI.Image> ().color = Color.white;
option2.GetComponent<UnityEngine.UI.Image> ().color = Color.white;
feedbackText.text = “”;
}
// Update is called once per frame
void Update () {
Hi, Brathnann. Sorry for no code tags in a previous post. And thanks for trying to help. The exact error:
NullReferenceException: Object reference not set to an instance of an object
Main.Start () (at Assets/Main.cs:22). And the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Main : MonoBehaviour {
public Text quizText;
public Text feedbackText;
public Button option1;
public Button option2;
// Use this for initialization
void Start () {
Debug.Log ("Start web service FROM MAIN METHOD");
quoteWS quiz = new quoteWS ();
//int quizId = quizNumber.text;
string question=quiz.showRandomQuiz().Replace("[","").Replace("]","");
string[] breakStringToArray = question.Split (',');//split result string into an array
string startQuote=breakStringToArray [0];
quizText.text = startQuote;
string answerOption1 = breakStringToArray [2];//set string for the first option
option1.GetComponentInChildren<Text> ().text = answerOption1;//set the button text to the answer option number 1
string answerOption2 = breakStringToArray [4];//set string for the first option
option2.GetComponentInChildren<Text> ().text = answerOption2;//set the button text to the answer option number 1
//reset text button colors and feedback text field
option1.GetComponent<UnityEngine.UI.Image> ().color = Color.white;
option2.GetComponent<UnityEngine.UI.Image> ().color = Color.white;
feedbackText.text = "";
}
// Update is called once per frame
void Update () {
}
}
Since you are declaring quiz before you make that call, quiz shouldn’t be null. Which is why I think there is something wrong in the method itself. If you Debug.log (quiz.showRandomQuiz());, does it give a null error on the debug line or do you get a returned string like you’d expect?
You should also be able to Debug.log(quiz) to make sure it isn’t null. Or just check if it’s == to null, but it shouldn’t be.
On Debug.Log(quiz.showrandomQuiz()); I got
"Null
UnityEngine.Debug:Log(Object) "
On Debug.Log(quiz); -
" quoteWS
UnityEngine.Debug:Log(Object) "
If i run same unity project with SOAP web service on a local machine - it runs. I did some research and found: SOAP and other web services only work for standalone builds. For Web Player builds, Web Services are disables for security reasons and so WWW is pretty much the only reliable option for transferring information via HTTP. I start to read about WWW as am completely new to Unity and web services. Is there any tutorial, by any chance, on how to do that? For now I have converted WSDL file (which is located now on Virtual Server not on local machine) into cs with Mono. Then generated from it dll. Copied both to Unity assets folder. In Plugins folder there are System.Web.dll and System.Web.Services.dll. I would be grateful for any suggestions. Thank you