2
\$\begingroup\$

I'm using unity and I've got some simple code that should work but doesn't for some reason and I'm stuck. Basically I am just trying to open up a website through a button click "On Click ()" in an application meant for android but it does not work. The code goes as follows.

public string url;

public void Website()
{
    Application.OpenURL(url);
}

public void Website2(string diffURL)
{
    Application.OpenURL(diffURL);
}

public void Website3()
{
    Application.OpenURL("http://www.google.com/");
}

As you can see I have three different variations of the same "Application.OpenURL" testing on three different buttons and all three do not work. Some more information:

  • I have paid close attention to format the websites properly with quotation marks and "http://"
  • I have already set the "Internet Access" to "require" in the player settings,
  • and interestingly enough, they work in the game window when testing, it is only when I upload the apk to my android device that it does not work anymore .

The buttons register I am pressing them as evident by the slight darkening of the button press when I press on my android device but nothing happens. I have seen this question asked around but no solution to my issue has been found. Are any other build settings wrong? Any other thing I should change in Unity? API level or what?(currently 19 on min and highest installed on target)

Is there some settings I should change on my android device to allow this to happen? Because as stated earlier it works when testing through the game window on unity but not on android.

(The function "Website3()" begun to work when I rebuilt the app. No changes were made. "Website1()" and "Website2()" however still do not work on android and they work perfectly fine on unity game window.)

\$\endgroup\$
  • \$\begingroup\$ Have you checked the logcat output at all to see if there’s an exception occurring at the point when you press the button? \$\endgroup\$ – Ed Marty Mar 8 at 15:45
  • \$\begingroup\$ @EdMarty how would I go about doing that? \$\endgroup\$ – EmperiumIce Mar 8 at 20:04
  • \$\begingroup\$ Search logcat on Google to find out more. \$\endgroup\$ – Ed Marty Mar 8 at 21:55
  • \$\begingroup\$ @EdMarty is there any particular error logs I should be checking for? There doesnt seem to be any debug checks on their documentation? \$\endgroup\$ – EmperiumIce Mar 8 at 22:48
  • \$\begingroup\$ Not sure really. Look for exceptions in the logs (eg. NullPointerExcrption, ActivityNotFoundException, NullReferenceException...). I would recommend just clearing the logs, pressing the button, and then immediately stopping the logs and see what shows up \$\endgroup\$ – Ed Marty Mar 8 at 22:54
2
\$\begingroup\$

I found out the problem! So when you type out url for Application.OpenURL there are quotation marks around the URL and so I typed in the url string values (url and diffURL) the same way in the inspector for the "Website1()" and "website2()" examples, with quotation marks and all ("http://www.google.com/"). For some reason it does not work that way. I took away the quotation marks in my user string values and boom it worked (http://www.google.com/). However this still doesn't answer why "Website3()" was not working initially and then did work after I rebuilt the app even though I changed nothing. I guess it doesn't matter too much anyway since it works now.

Essentially if you are going to input a url as a string value from the inspector as opposed to hard coded into the script then DO NOT write out the url with quotation marks surrounding it.

Hopefully this helps others who may face the same issue.

Thanks to EdMarty and DMGregory.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Please use answers to provide your solution, rather than editing the answer into the question. I've moved this answer out of your question and into this answer you thankfully left. Now you can also mark it as accepted. \$\endgroup\$ – doppelgreener Mar 11 at 10:48
  • \$\begingroup\$ Note that the reason you need quotation marks in code is to delineate the ends of the string literal. So if I want to print the text "Destroy(gameObject)", the compiler knows to treat the stuff in quotation marks as just literal text to print — and doesn't try to execute it as code to destroy my game object. When you're typing into an inspector, the field itself already delineates the string variable — so you don't need extra quotation marks to say "by the way, this is literal text, not programming code". \$\endgroup\$ – DMGregory Mar 11 at 11:38
0
\$\begingroup\$

Good for opening website to the browser

string baseUri = "https://myawesomewebsite.com"
Application.OpenURL(baseUri + "/faqs.html");
Application.OpenURL(baseUri + "/contactus.html");

Good for opening deployed app to the Play Store

string market = "market://details?id="
Application.OpenURL(market + Application.identifier);
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.