x


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); 

}

}

more ▼

asked Dec 20 '11 at 12:42 AM

Hannandh gravatar image

Hannandh
1 1 1 2

Old habits die hard, so, might as well start off doing things right and get your script formatted correctly in the code box so we can read it.

Dec 20 '11 at 12:49 AM Lo0NuhtiK

It looks right when I go in to edit it, so I don't know why it comes out like this, shouldn't be that hard to decipher...

Dec 20 '11 at 01:24 AM Hannandh

No problem, I'll try to make it easier to read now, I completely understand what you mean, I'm asking for help so there is no need to make it more difficult on who is helping me.

Dec 20 '11 at 01:43 AM Hannandh
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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)); 
// --------------------------------------------------------------
    }
}

:)

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).

more ▼

answered Dec 20 '11 at 01:56 AM

Zergling103 gravatar image

Zergling103
201 6 8 11

Thanks dude, I'll make sure not to make the mistake again :P

Dec 20 '11 at 02:00 AM Hannandh

Give me a +1 please :)

Dec 20 '11 at 02:01 AM Zergling103

How do I do that? Sorry, I only made this Profile to ask this certain question, I'm new to this, bare with me and I'll give you the +1 if you tell me how, god, I'm such a noob. -_-;

Dec 20 '11 at 02:07 AM Hannandh

See the thumb up icon by my answer? :) That.

Don't worry about looking like a noob. xD

Dec 20 '11 at 02:09 AM Zergling103

lol ... I'll give you one also

Dec 20 '11 at 02:10 AM Lo0NuhtiK
(comments are locked)
10|3000 characters needed characters left

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).

more ▼

answered Dec 20 '11 at 12:51 AM

save gravatar image

save
8.2k 22 31 62

Sorry, I really don't understand this, I'm just learning how to use Unity, I'm not too familiar with scripting or anything.

Dec 20 '11 at 01:25 AM Hannandh
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3315
x1937
x1937
x445

asked: Dec 20 '11 at 12:42 AM

Seen: 915 times

Last Updated: Dec 20 '11 at 02:10 AM