playMaker

Author Topic: How can I access a property from within a script?  (Read 1273 times)

velketor

  • Junior Playmaker
  • **
  • Posts: 69
  • Multimedia Marathon Man
    • Shawn Kilian Portfolio
How can I access a property from within a script?
« on: August 17, 2017, 03:07:13 PM »
Hello,

How can I access the url string from this script?  I can't seem to access it using Get Property, Set Property or Call Method like I usually can from other scripts.

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class MomentaryOpen: MonoBehaviour {
void Start()
{
StartCoroutine(GetText());
}

string authenticate(string username, string password)
{
string auth = username + ":" + password;
auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
auth = "Basic " + auth;
return auth;
}

IEnumerator GetText()
{
string authorization = authenticate("none", "password");
while (true)
{
string url = "http://myURLexample.com";


UnityWebRequest www = UnityWebRequest.Get(url);
www.SetRequestHeader("AUTHORIZATION", authorization);
yield return www.Send();

}
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How can I access a property from within a script?
« Reply #1 on: August 18, 2017, 02:18:37 AM »
Hi,

 the url variable is not accessible, by any external scripts, even without PlayMaker.

 you need to expose this url variable as a public variable of the class itself and then it becomes accessible. Like so:

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class MomentaryOpen: MonoBehaviour {

   public string url;

void Start()
{
StartCoroutine(GetText());
}

string authenticate(string username, string password)
{
string auth = username + ":" + password;
auth = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(auth));
auth = "Basic " + auth;
return auth;
}

IEnumerator GetText()
{
string authorization = authenticate("none", "password");
while (true)
{
this.url = "http://myURLexample.com";


UnityWebRequest www = UnityWebRequest.Get(this.url);
www.SetRequestHeader("AUTHORIZATION", authorization);
yield return www.Send();

}
}
}


the code above is pseudo, just to show how I declared url as a public variable of the class itself and use it then inside methods.

 You should do more reading on IEnumerator, cause I don't think it's a good idea to use a while(true) within a IEnumerator this way, the yield statement should be enough

check this: https://docs.unity3d.com/ScriptReference/WWW.html

you'll see that the logic is a lot more simple for www to work.

Bye,

 Jean