3d Menu Script Acting Up

I’ve had this bug for a while now, but I can’t pin it down after a couple of hours of going at it I figured it must be staring me in the face (and laughing).

I’m using 3d objects (Box colliders, Layers, Ray cast) to make my main menu. The first time in the scene everything works as expected; on off on off ad infinitum. However, once I go through campaign, select the save, load the game and go back through Application.LoadLevel to the title screen nothing happens. I use the raycast to match the name of the button, “Campaign” or “Options”. I put two prints in the code:

if (Physics.Raycast(ray.origin, ray.direction, out hit, 1000, layer))
			{
				
				audio.clip = menuSelection;
				audio.Play ();
				thingHit = hit.transform;
				print (hit.transform.name);                     //Will Always return "Campaign" when clicking on it
				yield return new WaitForSeconds(audio.clip.length);

				if(thingHit.name == "Campaign")
				{
					print ("camp");                                  //only happens on scene's first run
					
					if(Save1.gameObject.activeSelf)
					{
						optWord.gameObject.SetActive (true);
						highlight.gameObject.SetActive (false);
						Save1.gameObject.SetActive (false);
						Save2.gameObject.SetActive (false);
						Save3.gameObject.SetActive (false);
						del1.gameObject.SetActive (false);
						del2.gameObject.SetActive (false);
						del3.gameObject.SetActive (false);
						
					}else
					{
						highlight.gameObject.SetActive (true);
						optWord.gameObject.SetActive (false);
						Save1.gameObject.SetActive (true);
						Save2.gameObject.SetActive (true);
						Save3.gameObject.SetActive (true);
						del1.gameObject.SetActive (true);
						del2.gameObject.SetActive (true);
						del3.gameObject.SetActive (true);
					}
					
					
				}

I do have thingHit public, and i can see it work/not work as described–even though the hit.transform.name says it sees the blasted object. I’ve also tried comparing it to a transform saved to a variable with no avail.

I’d love an explanation of why it works the first time, but not after loading the scene again. I don’t have any “don’t destroy on load” used, so I’m sure its not related to that.

I can’t explain why it happens, but I’d offer a workaround: as you use GameObjects as menu items, propably using of OnMouseUpAsButton() method instead of Raycast will be better way to make buttons click logic. See documentation here: Unity - Scripting API: MonoBehaviour.OnMouseUpAsButton()

This seems to work, i just have to clean the script up. Thanks!

…I’d still like to know why though :expressionless: