GetComponent isn't working with Unity 5.2 - c#

I'm trying to follow along to this tutorial http://wiki.unity3d.com/index.php?title=Server_Side_Highscores
But it hasn't been updated to Unity 5.2 and I can't get the GetComponent to work no matter what I try.
IEnumerator GetScores()
{
GameObject text = new GameObject ("Loading Scores");
text.GetComponent<GUIText>();
WWW hs_get = new WWW(highscoreURL);
yield return hs_get;
if (hs_get.error != null)
{
print("There was an error getting the high score: " + hs_get.error);
}
else
{
GetComponent<GUIText>(hs_get.text); //Line of code not working
// this is a GUIText that will display the scores in game.
}
}
The error is: Assets/HSController.cs(46,25): error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments
I can't seem to get GetComponent working any help would be greatly appreciated

Have you tried
text.text = hs_get.text;
I'm not entirely sure what your code is supposed to do, but I don't know why you're using GetComponent. If you don't want to reuse the "text" variable, then you could maybe try something like
hsText = GetComponent <GUIText> ();
hsText.text = hs_get.text;
I don't know if I'm addressing the problem but I hope this helps!

Related

How do I use Unity Web Requests for streaming audio... I want to use a cloud based TTS

I'm trying to use a cloud TTS within my Unity game.
With the newer versions (I am using 2019.1), they have deprecated WWW in favour of UnityWebRequest(s) within the API.
I have tried the Unity Documentation, but that didn't work for me.
I have also tried other threads and they use WWW which is deprecated for my Unity version.
void Start()
{
StartCoroutine(PlayTTS());
}
IEnumerator PlayTTS()
{
using (UnityWebRequestMultimedia wr = new UnityWebRequestMultimedia.GetAudioClip(
"https://example.com/tts?text=Sample%20Text&voice=Male",
AudioType.OGGVORBIS)
)
{
yield return wr.Send();
if (wr.isNetworkError)
{
Debug.LogWarning(wr.error);
}
else
{
//AudioClip ttsClip = DownloadHandlerAudioClip.GetContent(wr);
}
}
}
The URL in a browser (I used Firefox) successfully loaded the audio clip allowing me to play it.
What I want it to do is play the TTS when something happens in the game, it has been done within the "void Start" for testing purposes.
Where am I going wrong?
Thanks in advance
Josh
UnityWebRequestMultimedia.GetAudioClip automatically adds a default DownloadHandlerAudioClip which has a property streamAudio.
Set this to true and add a check for UnityWebRequest.downloadedBytes in order to delay the playback before starting.
Something like
public AudioSource source;
IEnumerator PlayTTS()
{
using (var wr = new UnityWebRequestMultimedia.GetAudioClip(
"https://example.com/tts?text=Sample%20Text&voice=Male",
AudioType.OGGVORBIS)
)
{
((DownloadHandlerAudioClip)wr.downloadHandler).streamAudio = true;
wr.Send();
while(!wr.isNetworkError && wr.downloadedBytes <= someThreshold)
{
yield return null;
}
if (wr.isNetworkError)
{
Debug.LogWarning(wr.error);
}
else
{
// Here I'm not sure if you would use
source.PlayOneShot(DownloadHandlerAudioClip.GetContent(wr));
// or rather
source.PlayOneShot((DownloadHandlerAudioClip)wr.downloadHandler).audioClip);
}
}
}
Typed on smartphone so no warranty but I hope you get the idea

Can't get variable value outside task scope Firebase unity

I'm here getting started with Firebase inside Unity. I figured out how to get data from my database. But the problem is that I can't get the value outside of the task that I am creating to get the data.
Here is the function that I am using, so you guys know what I am talking about:
public double GetBankBalance()
{
double bankBalance = 0;
Router.PlayerMoney("ymO6Hyl8WjbuPGaWV7FksbVABGb2").GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.Log("Error in GetBankBalance()");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
bankBalance = Convert.ToDouble(snapshot.Child("bankBalance").Value);
}
Debug.Log(bankBalance);
SetBankBalance(bankBalance);
});
Debug.Log(bankBalance);
return bankBalance;
}
I'll explain it a bit:
The "Router.PlayerMoney is just the path to the values that I need. The problem here is that inside the task the Debug.Log(bankBalance) returns the correct value but just outside (the Debug.Log above my return) the value is 0 as I gave bankBalance the value at the beginning of the function.
I can't seem to figure out what the problem is. I tried instance variables but that won't work.
Does anyone have an idea what might be the problem?

StartCoroutine (with WWW) does not work on Android but it does on the editor - Unity

I am working on an application for my web service. I work with LitJson, I finished the application but at the moment of exporting it to test it in my android does not work related to WWW IEnumerators.
For example, a button that starts the session, only the function that calls the IEnumerator works, but the IEnumerator itself does not work, and I have checked it by placing an alert message that it creates for the application.
This is the function that the button calls:
public void LogIn() {
UserName = GameObject.Find("inputUsuario").transform.GetChild(2).gameObject.GetComponent < Text > ().text;
Password = GameObject.Find("inputPassword").transform.GetChild(2).gameObject.GetComponent < Text > ().text;
Debug.Log("Username:" + UserName + "And Password:" + Password);
StartCoroutine(LogInWWW());
}
And this is the IEnumerator:
public IEnumerator LogInWWW() {
WWW www = new WWW(UrlLogin + "?userName=" + UserName + "&password=" + Password);
yield return www;
print(www.text);
if (www.error == null) {
ProcessjsonLogin(www.text);
} else {
Debug.Log("ERROR: " + www.error);
}
}
I must add that there is no error in the debug console on Android.
Also I must clarify that to the URL I have added the prefix "http://" since it is a solution that give in the questions that I have seen.
Well guys, sometimes the errors are rare but in short, I managed to get to the error thanks to "trial and error", the problem was that I tried to get the value of UserName and Password using GetChild, like the following code:
`Password = GameObject.Find("inputPassword").transform.GetChild(2).gameObject.GetComponent<Text>().text;`
For some reason on Android the object does not exist, I learned this because I was testing until part of the code I got the problem. Then I decided to change the code to find the text in a more specific way, changing the text object by inputUserTextObj, the same with the Password object and changing the code by:
UserName = GameObject.Find("inputUsuarioTextObj").gameObject.GetComponent<Text>().text;
Password = GameObject.Find("inputPasswordTextObj").gameObject.GetComponent<Text>().text;
As I said, I knew that the problem was not the IEnumerator, but it seems too strange that Debugging did not show me any error since it was a typical error of "GameObject is Null" and I could even swear to them by my life that Debug the application and I never showed the said error
But hey, thank you very much for your time guys, really feel the support and forgive me for taking your time.
When you instantiate a new WWW(strung Url) it implicitly starts the request as well but it is not sync operation, it is async so you have to check www.isDone before you can use www.text. I think it works on Unity Editor properly because it just has a faster connection.
public IEnumerator Request(){
using (var testRequest = new WWW(#"URL")) {
var totalWaited = 0;
var timeOuted = false;
while (testRequest.isDone == false) {
yield return new WaitForSeconds(1);
totalWaited++;
if (totalWaited > timeOutInSeconds) {
timeOuted = true;
}
}
if (timeOuted) { Debug.LogError("TimeOut"); yield break; }
yield return testRequest.text;
}

Unity Error CS0126

I'm sure it's something stupid simple, but I can't get it figured out. The following code:
public GameObject AISelectCannon() {
Debug.Log("AISelectCannon called");
GameObject desiredCannon = AIController.selectCannon(0);
if (desiredCannon.tag.Contains("Cannon")) return;
m_SelectedCannon = desiredCannon;
aiSelectionPending = true;
return m_SelectedCannon;
}
is throwing the following error:
Scripts/CubeContainer.cs(61,59): error CS0126: An object of a type convertible to `UnityEngine.GameObject' is required for the return statement
(GameObject desiredCannon.... is line 61)
Other relevant information from AIController:
public static GameObject selectCannon(int side) {
So yeah, any ideas?
Your first return statement isnt returning anything, it just says return
if (desiredCannon.tag.Contains("Cannon")) return // return something

Using yield WaitForSeconds

I might be asking something that's extremely obvious and I have overlooked something, but I'm trying to create a pause before it does something.
I have seen this being used in many places online -
yield WaitForSeconds(2);
However I get a syntax error of,
"Error CS1528: Expected ; or = (cannot specify constructor arguments
in declaration) (CS1528) (Assembly-CSharp)
Which confuses me as im not really sure what yield as a keyword really means or does, and im under the assumption that WaitForSeconds is a class of its on with the "2" being in the constructor (not in a declaration) any help would be appreciated. thanks!
What you want is t use an IEnumerator.
IEnumerator Example()
{
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
Then you will ask: How do I call this?
void Start()
{
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(2.0F));
print("Before WaitAndPrint Finishes " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime)
{
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
I just read the link Jon Skeet posted on the comments, I too recommend it, it has pretty valuable information.
You are using the Javascript code for Unity, and try it in the C# language, that´s why you get the error message.
If you click on the C# language selector on a page like http://docs.unity3d.com/ScriptReference/WaitForSeconds.html
you will get the following example code for C#:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
IEnumerator Example() {
print(Time.time);
yield return new WaitForSeconds(5);
print(Time.time);
}
}

Categories