- Home /
Getting strings from WWW
Hey so I'm currently working on a unity project where Unity needs to constantly check for a new string from a website using WWW. As of now I created a script that adds and destroys a component based on whether or not the component has finished loading the webpage and reading the text. Does anyone have any cleaner ways of making a Android phone communicate with unity to send simple strings? heres my code at the moment
//for Very early testing
var card:GameObject;
var currentCard:String;
var places: GameObject[];
var getCard: GetInfo;
var controller:P1Controller;
var done:boolean = false;
function Start () {
//gets Main controller
controller = this.gameObject.GetComponent(P1Controller);
//sets card locations
places = controller.getPlaces();
//set current card to main controllers car
currentCard = controller.card;
//create a new check for a string
getCard = this.gameObject.AddComponent(GetInfo);
//load card for testing purposes
card = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Tank.prefab", GameObject);
//wait for WWW request to finish
while(!getCard.isDone())
{
yield WaitForSeconds (.1);
}
//set card text and delete request object
currentCard = getCard.getText();
Destroy(this.gameObject.GetComponent(GetInfo));
done = true;
}
function Update () {
if(done)
{
if(getCard.isDone())
{
//Destroy Object just in case
Destroy(this.gameObject.GetComponent(GetInfo));
if(currentCard == controller.card)
{
//if cards are equal check again for changes
CheckAgain();
}
else
{
//create new object test
Instantiate(card, places[2].transform.position, places[2].transform.rotation);
controller.card = currentCard;
}
}
}
}
function CheckAgain()
{
//create a new check for a string
getCard = this.gameObject.AddComponent(GetInfo);
//wait for WWW request to finish
while(!getCard.isDone())
{
yield WaitForSeconds (.1);
}
//set card text and delete request object
currentCard = getCard.getText();
Destroy(this.gameObject.GetComponent(GetInfo));
Debug.Log("YOPP");
}
thanks all
Comment
Your answer
Follow this Question
Related Questions
Coroutine needed here? 1 Answer
WWW, RESTful Service, and Threading 1 Answer
Coroutines and Static function 1 Answer
Yielding with WWW in Editor 9 Answers