I have a script that essentially creates a menu. The menu has a number of tabs to sort through information. One of these tabs (the credits tab) spawns in a prefab object that displays a credits list. When I click on one of the other tabs, I want to destroy the prefab so it doesn’t get in the way, but for some reason the code isn’t working. There are no error messages being displayed so I have no idea what the problem is. my code is as follows
public class TabBehavior : MonoBehaviour {
public AudioClip pageTurn;
public GameObject bookText;
public GameObject Creditslist;
public GameObject testRef;
// Use this for initialization
void Start () {
bookText = GameObject.FindGameObjectWithTag (Tags.BookText);
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
print ("clicked in tab!");
AudioSource.PlayClipAtPoint (pageTurn, Vector3.zero);
if(this.gameObject.tag == Tags.MapTab){
bookText.gameObject.GetComponent<GUIText>().text = "Credits";
testRef = Instantiate (Creditslist) as GameObject;
}
if(this.gameObject.tag == Tags.OptionsTab){
bookText.gameObject.GetComponent<GUIText>().text = "Options";
GameObject.Destroy (testRef);
}
if(this.gameObject.tag == Tags.SaveLoadTab){
bookText.gameObject.GetComponent<GUIText>().text = "Save Load";
GameObject.Destroy (testRef);
}
if (this.gameObject.tag == Tags.CluesTab) {
bookText.gameObject.GetComponent<GUIText>().text = "Clues";
GameObject.Destroy (testRef);
}
if (this.gameObject.tag == Tags.PeopleTab) {
bookText.gameObject.GetComponent<GUIText>().text = "People";
GameObject.Destroy (testRef);
}
}
}
Can anyone tell me what the problem is? Also, I know there are probably much cleaner ways of doing this setup, but I need this done by tomorrow, so I don’t have time to rewrite anything, especially since I need this function working for one of the other tabs on this timeframe. So please limit advice to helping me make this work instead of suggestions on how to do it better, at least for now since I don’t have time to revamp anything. Thank you in advance. Alsp, my prefab keeps spawning in behind the menu (also a prehab) Any idea how to fix that? the game is 2d, so I’ve tried moving the prefab closer to the camera and changing the layer, but it keeps showing up behind the other prefab. Thanks in advance.