How To Invoke RayCastAll?

Let me explain. I want to know how to invoke a RayCastAll script of mine to activate after a specific amount of time after I collect a certain powerup. Then deactivate after 2-3 seconds or so, and then repeat the process. The RayCastAll script of mine destroys objects in front of me when I attach it to the Main Camera, so I’m trying to get it to activate only after a certain amount of time after I collect a powerup of mine(I just restated what I just type earlier, sorry). So how can I achieve this? No invoke tutorials that I found are really that helpful, and there are very few of them on the internet and here; so I hope someone can help me. I’ve already tried all the scripts and references that I could find with the help of my own knowledge, but I haven’t been able to come up with much.

If you don’t fully understand what I want/need to happen, then let me put it in steps. Step 1. Player grabs powerup, timer starts. Step 2. Timer ends, causing my RayCastAll script to activate for 2-3 seconds. Step 3. 2-3 seconds are over, RayCastAll script deactivates, and that’s it. Then you just repeat the steps. Here’s my scripts to help you guys see how I set things up:

RayCastAll script:

public class DestroyObstacle : MonoBehaviour {


	void Update() {
		RaycastHit2D[] hits;
		hits = Physics2D.RaycastAll(transform.position, transform.forward, 100.0F);

		Debug.DrawRay(transform.position, transform.forward * 100, Color.red);

		int i = 0;
		while (i < hits.Length) {
			RaycastHit2D hit = hits*;*
  •  	Renderer renderer = hit.collider.renderer;*
    
  •  	if (renderer) {*
    
  •  		renderer.material.shader = Shader.Find("Transparent/Diffuse");*
    
  •  		Color color = renderer.material.color;*
    
  •  		color.a -= 0.3F;*
    
  •  		renderer.material.color = color;*
    
  •  		Invoke("LaunchProjectile", 2);*
    
  •  	}*
    
  •  	i++;*
    
  •  	Destroy(hit.collider.gameObject);*
    
  •  }*
    
  • }*
    }
    public class PowerupScript : MonoBehaviour {

  • void OnTriggerEnter2D(Collider2D other)*

  • {*

  •  		if (other.tag == "Player") {*
    
  •  				ControllerScript playerScript = other.gameObject.GetComponent<ControllerScript> (); // not sure about the syntax here...*
    
  •  				if (playerScript) {*
    
  •  		                // call function on playerScript to set player to invincible*
    
  •  						playerScript.SetInvincible ();*
    
  •  		                // We speed up the player and then tell to stop after a few seconds*
    
  •  		                playerScript.SpeedUp();*
    
  •  						// We speed up the player and then tell to stop after a few seconds*
    
  •  						//playerScript.coeffSpeedUp = 5.5f;*
    
  •  						//StartCoroutine (playerScript.StopSpeedUp ());*
    
  •  				}*
    
  •  				Destroy (gameObject);*
    
  •  		}*
    
  •  }*
    
  • }*
    I also have this new invoke script that I just made(it’s incomplete):
    ublic class InvokeScript : MonoBehaviour {

  • public int secs2Wait = 3;*

  • public int totalSec = new int[3];*

  • void Start()*

  • {*

  •  InvokeRepeating ("TimerInvoke", 1, 1);*
    
  • }*

  • void TimerInvoke()*

  • {*

  •  if (totalSec [0] < secs2Wait)*
    
  •  				totalSec [0]++;*
    
  •  else*
    
  •  	CancelInvoke("TimerInvoke");*
    
  • }*
    }
    If you want my controller script, please feel free to ask. :smiley:
    EDIT: Wow this question is old, and still nobody knows a solution to this? This script and what I wanted to do can still be very useful to me. So if somebody knows, please feel free to answer.

I takes some time to puzzle through what you are asking. Rather than InvokeRepeating, structure things this way. At the top of the file put:

 private bool canRaycastAll = false;

Put something like this at the top of Update():

 if (!canRaycastAll)
     return;

Then when you get your power up, you would do:

 canRaycastAll = true;
 Invoke("CancleRaycastAll", 2.5f) 

An CancleRaycastAll would be:

void CancleRaycastAll() {
    canRaycastAll = false;
}

There are other ways of doing what you want. This is only one of them, but probably the easiest to implement in your code above. The one sticking point may be where you detect the collision. If it is is another script on another game object, there is more to do to get this working.