Problem doing a script.

This is the error message I get.

Assets/Scipt0/Gun.js(22,20): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(UnityEngine.Transform, UnityEngine.Vector3, UnityEngine.Vector3)’ was found.

This is my script.

var sparks : Transform;

var ammo : int = 100;

var fireRate : float = 0.2;

private var nextFire : float = 0.0;

function Update () {

if(Input.Getbutton("Fire1")){

	if(Time.time > nextFire){

		nextFire = Time.time + fireRate;
		fire();
		
	}
}

}

function fire(){

var range = Mathf.Infinity;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

var hit : RaycastHit;

if (Physics.Raycast (ray, hit, range)) {

	Instantiate(sparks, hit.point, hit.normal); 

}

}

Here:

var sparks : Transform;
var ammo : int = 100;
var fireRate : float = 0.2;
private var nextFire : float = 0.0;

function Update () {
    if(Input.Getbutton("Fire1") && Time.time > nextFire) {
         nextFire = Time.time + fireRate;
         fire();
    }
}

function fire() {
    var range = Mathf.Infinity;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;

    if (Physics.Raycast (ray, hit, range)) {
// --------------------------------------------------------------
        Instantiate(sparks, hit.point, Quaternion.LookRotation(hit.normal)); 
// --------------------------------------------------------------
    }
}

:slight_smile:

Instantiate requres a special rotation structure called a Quaternion be used to describe it’s starting rotation. Quaternion.LookRotation() does basically what you intended by figuring out what rotation is requried to look at a point in space when standing at (0,0,0).

Instantiate takes three parameters, the object, the position and the rotation. The rotation has to be a quaternion. Is there a reason you’re using the normal from hit? Otherwise switch it to Quaternion.identity or the desired Quaternion.Euler(x, y, z).