Raycasting issue

Hey Everyone, new to unity and i am hoping for someone to help me, have searched a while for an answer but cant seem to find anything that can help with my problem…

I am using raycasts to make a level select scene which has 6 levels to choose. when you load the level you want, it loads a preview of the level then you decide to play it or not.

my problem happens while testing my game i choose the level which takes me to the preview scene but for some reason the raycast is still active/casting while the preview scene loads and then starts the level.

I ASS.U.ME its to do with my level preview collider being in the same position as my play collider on different scenes, but i cant figure out why my raycasts are still active coming from one scene to the other.

does anyone have an idea how to get the raycast to only work once ie when i make my first collision, stop casting the ray?

thanks

#pragma strict

function Update () {
	
    var hit : RaycastHit;

    var touches = Input.touches;

    for (var touch : Touch in touches){

        var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y, 0));

        if (Physics.Raycast (ray,hit)){
		
    
        
        
        if ( hit.collider.gameObject.tag == "level01" )
   			 {
    			Application.LoadLevel (04);
    			return;
    		 }
    	
    	if ( hit.collider.gameObject.tag == "level02" )
   			 {
    			Application.LoadLevel (06);
    			return;
    		 }
    	
    	if ( hit.collider.gameObject.tag == "level03" )
   			 {
    			Application.LoadLevel (08);
    			return;
    		 }
    		 
        if ( hit.collider.gameObject.tag == "level04" )
   			 {
    			Application.LoadLevel (10);
    			return;
    		 }
    	
    	if ( hit.collider.gameObject.tag == "level05" )
   			 {
    			Application.LoadLevel (12);
    			return;
    		 }
    		 
        if ( hit.collider.gameObject.tag == "level06" )
   			 {
    			Application.LoadLevel (14);
    			return;
    		 }


this is my levelPlay script

#pragma strict

function Update () {
	
    var hit : RaycastHit;

    var touches = Input.touches;

    for (var touch : Touch in touches){

        var ray = Camera.main.ScreenPointToRay (Vector3(touch.position.x, touch.position.y, 0));

        
        if (Physics.Raycast (ray,hit)){
		
    	// what did the raycast hit ?
    	Debug.Log( "ray hit (name): " + hit.collider.gameObject.name);
    	Debug.Log( "ray hit (tag): " + hit.collider.gameObject.tag );
        
        
        if ( hit.collider.gameObject.tag == "playLevel01" )
   			 {
    			Application.LoadLevel (05);
    			return;
    		 }
    	
    
    	
    	 
    	 
    	if ( hit.collider.gameObject.tag == "playLevel02" )
   			 {
    			Application.LoadLevel (07);
    			return;
    		 }
    	 
    	if ( hit.collider.gameObject.tag == "playLevel03" )
   			 {
    			Application.LoadLevel (09);
    			return;
    		 }
    	
    	if ( hit.collider.gameObject.tag == "playLevel04" )
   			 {
    			Application.LoadLevel (11);
    			return;
    		 }
    	
    	if ( hit.collider.gameObject.tag == "playLevel05" )
   			 {
    			Application.LoadLevel (13);
    			return;
    		 }
    		 
        if ( hit.collider.gameObject.tag == "playLevel06" )
   			 {
    			Application.LoadLevel (15);
    			return;
    		 }

Are you trying to not use any keys or buttons? You could use:

if (Input.GetKeyDown ("space"))
{
//do your raycast and other code here
}

Otherwise you’ll still be raycasting as long as that script is on an object and the ray is being created during update.

I’m guessing on the scene you move into you have a prefab gameObject (or just on a gameObject) that now has the script attached to it and thus is creating rays.