Question by
jamesatjaminit · May 21, 2020 at 10:05 AM ·
playervector3string
Convert String to Vector3
Hi, I'm new to Unity so please bare with me. I'm trying to make a multiplayer game and I've got everything server side and pretty much everything client side working. The only problem is downloading and setting the player position. My code is below and may have a couple of errors in (since I've been trying different ways) but the question that I'm asking is how can I get the string that I download to the players position.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityEngine;
using UnityEngine.Networking;
public class UpdateClientPos : MonoBehaviour
{
public int count;
public string urlget;
public string data;
public int lines;
public string userid;
public bool useridvalid;
public int dataint;
public bool useint;
public bool usevec;
public string datavec;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (count > 100)
{
while (useridvalid == false)
{
urlget = "https://example.com/getline.html";
StartCoroutine(GetText());
userid = data;
if (userid == DeviceUniqueIdentifier)
{
}
else
{
useridvalid = true;
}
}
urlget = "https://example.com/data/users/" + userid + ".txt";
usevec = true;
StartCoroutine(GetText());
GameObject.Find("NetworkPlayer").transform.position = datavec;
count = 0;
}
else
{
count = count + 1;
}
}
IEnumerator GetText()
{
UnityWebRequest www = UnityWebRequest.Get(urlget);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
//Debug.Log(www.error);
}
else
{
if (useint == true)
{
dataint = int.Parse(www.downloadHandler.text);
useint = false;
}
if (usevec == true)
{
datavec = www.downloadHandler.text;
usevec = false;
}
else
{
data = www.downloadHandler.text;
}
}
}
}
A sample of the string which is downloaded by the script is below
(1.2, 0.6, -2.6)
The error that I'm getting is
Cannot implicitly convert type 'string' to 'UnityEngine.Vector3'
I have also tried implementing: https://answers.unity.com/questions/1134997/string-to-vector3.html however it didn't work
Comment