Search Unity

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

Discussion in 'Editor & General Support' started by maubar, Oct 14, 2015.

  1. maubar

    maubar

    Joined:
    May 27, 2015
    Posts:
    128
    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:

    Code (JavaScript):
    1.  
    2.  
    3.  
    4.  
    5.     var EffectsObject: GameObject;
    6.     // var someParticleEmitter : GameObject;
    7.     var emitter = GetComponent.<ParticleEmitter>();
    8.     var TheDamage = 100;
    9.  
    10.  
    11.  
    12.  
    13.  
    14. function Update () {
    15.  
    16.     var hit : RaycastHit ;
    17.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    18.    
    19.     if (Input.GetMouseButtonDown(0))
    20.     {
    21.         GetComponent.<AudioSource>().Play();
    22.        
    23.        
    24.         emitter.emit = true;
    25.        
    26.         if (Physics.Raycast (ray,hit,100))  
    27.         {
    28.            
    29.             print ("disparo");
    30.             var particleClone = Instantiate (EffectsObject, hit.point, Quaternion.LookRotation(hit.normal));
    31.            
    32.             Destroy(particleClone.gameObject, 3);  // espera 5 segundosay destruye
    33.             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    34.             // Debug.DrawLine (ray.origin, hit.point);  
    35.         }
    36.  
    37.     }
    38.             else
    39.         {
    40.        
    41.         emitter.emit = false;
    42.         print ("-------");
    43.        
    44.         }
    45.    
    46. }
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Line 7:

    Code (csharp):
    1. var emitter = GetComponent.<ParticleEmitter>();
    You cannot use GetComponent() in an initializer. Use Awake() or Start() instead.
     
    karl_jones likes this.
  3. maubar

    maubar

    Joined:
    May 27, 2015
    Posts:
    128
    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

    Code (JavaScript):
    1. function Update () {
    2.  
    3.     var hit : RaycastHit ;
    4.     var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
    5.    
    6.     if (Input.GetMouseButtonDown(0))
    7.     {
    8.         GetComponent.<AudioSource>().Play();
    9.         emitter.GetComponent(ParticleEmitter).emit = true;
    10.        
    11.         if (Physics.Raycast (ray,hit,100))  
    12.         {
    13.             print ("shoot");
    14.             var particleClone = Instantiate (EffectsObject, hit.point, Quaternion.LookRotation(hit.normal));
    15.            
    16.             Destroy(particleClone.gameObject, 1);  // espera 5 segundosay destruye
    17.             hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    18.             // Debug.DrawLine (ray.origin, hit.point);  
    19.         }
    20.  
    21.     }
    22.             else
    23.         {  
    24.         emitter.GetComponent(ParticleEmitter).emit = false;
    25.         print ("nothing");  
    26.         }
    27. }
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    That works, too. But remember that you're now calling GetComponent() each frame, so that's a bit expensive.
     
  5. maubar

    maubar

    Joined:
    May 27, 2015
    Posts:
    128
    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....
    .
    gotcha scene 1.jpg
     
  6. maubar

    maubar

    Joined:
    May 27, 2015
    Posts:
    128
    low polygons OCULUS vr game