Certain scripts that are attached does not work after build, scene change and closing unity
I am completely new to Unity. I made a UI for a game and attached a few scripts to the canvas. Some of the scripts works as expected while others work right after attaching to the canvas but not after build/scene change/closing Unity.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class gotoRoom : MonoBehaviour {
public Button yourButton;
GameObject pan;
void Start () {
pan = GameObject.Find ("roomPanel");
Button btn = yourButton.<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
GameObject.Find("topPanel").SetActive(false);
pan.SetActive(true);
}
}
The above code works when i click on the 'room' button.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class gotoWindow : MonoBehaviour {
public Button yourButton;
GameObject pan;
void Start () {
pan = GameObject.Find ("windowPanel");
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick(){
GameObject.Find("topPanel").SetActive(false);
pan.SetActive(true);
}
}
The following code does not show the 'window panel' on clicking the 'window' button. What am i doing wrong here? Please help!
yourButton is already a Button, why use GetComponent() ? You could simply do : void Start () { pan = GameObject.Find ("windowPanel"); yourButton.onClick.AddListener(TaskOnClick); } I believe TaskOnClick() must be public to be added as a Listener.
Thank you Sir. I changed the button script. I think it is a problem with my system as the same script works on my friend's system.
Your answer
Follow this Question
Related Questions
Is there a way I can make one script for all my buttons in my game? 1 Answer
How can i place subjects in different positions? 0 Answers
How to send event to my canvas via script 0 Answers
Always pickup closest item? 0 Answers
why the Timer isn't working 0 Answers