How to instantiate gameobject on raycast collision.

Hi I need to know how to instantiate a gameobject on raycast collision. For example, I have a game that a robot shoots rockets at my player, but I don’t want the robot to shoot the rockets till my player hits a raycast collider, so I need to know how to on a raycast collision,instantiate the rockets. And if you can I want the rockets to never sop instantiating. And if you can I would like it if the rockets instantiated every 5 seconds. Thanks so much.

I don’t know what you mean by “raycast collider”, but suppose you want to fire the rockets only when the player enters some area - is it correct? If so, you should use a trigger: create a cube, adjust its dimensions to cover the area, set the checkbox Is Trigger and then uncheck the Mesh Renderer to make the trigger invisible. Add this script to the trigger (ex-cube):

// script ZoneTrigger.js:
var playerInside = false; // becomes true when the player is inside the zone

function OnTriggerEnter(other: Collider){
  if (other.tag == "Player"){ // remember to tag the player as Player
    playerInside = true;
  }
}

function OnTriggerExit(other: Collider){
  if (other.tag == "Player"){
    playerInside = false;
  }
}

Then place this code in your robot script:

var zoneScript: ZoneTrigger; // drag the trigger object here
var shotInterval: float = 5; // shot interval in seconds
private var nextShot: float = 0;

function Update(){
  if (zoneScript.playerInside && Time.time > nextShot){
    nextShot = Time.time + shotInterval;
    // place here the code to fire the rocket
  }
}