works perfect ..... but always show this error

hi, i mademy new game, raycast and particle system shooting game
works great… like i need.
but always show this error…
pleasehelp

ArgumentException: GetComponentFastPath can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
UnityEngine.Component.GetComponent[ParticleEmitter] ()
raycastshooting…ctor () (at Assets/Raycast mau final unity5/Scripts/raycastshooting.js:7)

here is my code:

    var EffectsObject: GameObject;
    // var someParticleEmitter : GameObject;
    var emitter = GetComponent.<ParticleEmitter>();
    var TheDamage = 100;





function Update () {

    var hit : RaycastHit ;
    var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
   
    if (Input.GetMouseButtonDown(0))
    {
        GetComponent.<AudioSource>().Play();
       
       
        emitter.emit = true;
       
        if (Physics.Raycast (ray,hit,100))   
        {
           
            print ("disparo");
            var particleClone = Instantiate (EffectsObject, hit.point, Quaternion.LookRotation(hit.normal));
           
            Destroy(particleClone.gameObject, 3);  // espera 5 segundosay destruye
            hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
            // Debug.DrawLine (ray.origin, hit.point);   
        }

    }
            else
        {
       
        emitter.emit = false;
        print ("-------");
       
        }
   
}

Line 7:

var emitter = GetComponent.<ParticleEmitter>();

You cannot use GetComponent() in an initializer. Use Awake() or Start() instead.

1 Like

thanks Blizz …

i try you said… but dont know a lot of new javas cript unity 5…
but i fix it this way …

here is the code …
i apreciatte your help. thanks so much

function Update () {

    var hit : RaycastHit ;
    var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
   
    if (Input.GetMouseButtonDown(0))
    {
        GetComponent.<AudioSource>().Play();
        emitter.GetComponent(ParticleEmitter).emit = true;
       
        if (Physics.Raycast (ray,hit,100))   
        {
            print ("shoot");
            var particleClone = Instantiate (EffectsObject, hit.point, Quaternion.LookRotation(hit.normal));
           
            Destroy(particleClone.gameObject, 1);  // espera 5 segundosay destruye
            hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
            // Debug.DrawLine (ray.origin, hit.point);   
        }

    }
            else
        {   
        emitter.GetComponent(ParticleEmitter).emit = false;
        print ("nothing");   
        }
}

That works, too. But remember that you’re now calling GetComponent() each frame, so that’s a bit expensive.

but i dont know how to do it … in fact, this works but i dont knowhow … just testing jejejeje
and the game is very, very low polygons, for oculus VR …
but someday i willmake a great game and moregraphics quality…
please could you give another idea …?

here by the way is a picture of my game…
.

low polygons OCULUS vr game