hi! As i am a newbie and i don’t know how can i create a hypertext link button in unity. I have added the link but the button is not working. I am sharing my code and would love to have assistance from my seniors over here, so i may go through this task.
Your assistance will be highly appreciated. For convenience, I am adding my code so you can check where i am lagging with the code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class linkclick : MonoBehaviour
{
public void linkFunc()
{
Application.OpenURL (" https://scanmykitchen.com/vitamix-5200-vs-5300/ "); //[Adding this link behind a button or text, right now this code has an error]
}}
Make sure your script has the same name as the class: linkclick
In the unity editor, drag the script to the button and on the button create a click event, select the method call as a “linkclick”. You could also use a mousehandler to automate this.
I noted that your URL had trailing spaces, I corrected it below.
YOUR CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class linkclick : MonoBehaviour
{
public void linkFunc()
{
Application.OpenURL("https://scanmykitchen.com/vitamix-5200-vs-5300/"); //Fixed url
}
}
BETTER CODE: (note just drag this onto the button, don’t create a click event in the editor.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class linkclick : MonoBehaviour, IPointerClickHandler
{
public void OnPointerClick(PointerEventData pointerEventData)
{
Application.OpenURL("https://scanmykitchen.com/vitamix-5200-vs-5300/");
}
}
just a note that you should consider naming your classes/Methods with camelcase or something similar, they shouldn’t start with lower case names for readability etc. It’s better to learn that early than to go back and change it later on.
Also, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run?
what are the values of the variables involved? Are they initialized?
Knowing this information will help you reason about the behavior you are seeing.