Search Unity

Prefab Instantiate is null. Spawning Prefabs?

Discussion in 'Scripting' started by Artimese, Jul 20, 2010.

  1. Artimese

    Artimese

    Joined:
    Nov 22, 2009
    Posts:
    794
    I have this issue where my gun shoots using raycast and the particles that appear is basically just a simple detonator (cloud of smoke), the detonator (cloud of smoke) will destroy itself once it hits a collider (to prevent it to hit again anywhere else), but if it destroys itself, then it cannot be used again!

    So my issue is I can shoot my weapon once, and it will work perfectly fine, and show the particles/cloud of smoke, however if i shoot it again, it wont show the particles/cloud of smoke because its been destroyed.

    I get this error which I understand:
    Code (csharp):
    1. NullReferenceException: The prefab you want to instantiate is null.
    I know it means that the script cannot access the prefab because its destroyed, my question is how do I spawn another one after its destroyed?

    This is the script for the actual detonator
    Code (csharp):
    1. // The reference to the explosion prefab
    2. var explosion : GameObject;
    3.  
    4. function OnCollisionEnter (collision : Collision) {
    5.  
    6.    // Instantiate explosion at the impact point and rotate the explosion
    7.    // so that the y-axis faces along the surface normal
    8.    var contact : ContactPoint = collision.contacts[0];
    9.    var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
    10.     Instantiate (explosion, contact.point, rotation);
    11.    //variable for your particle prefab that you created and saved as a prefab.
    12. //drag that particle prefab here in the inspector
    13.  
    14. // And kill our selves
    15.   Kill ();    
    16. }
    17.  
    18. function Kill () {
    19.     // Stop emitting particles in any children
    20.     var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter);
    21.     if (emitter)
    22.     emitter.emit = false;
    23.  
    24.     // Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
    25.     transform.DetachChildren();
    26.        
    27.     // Destroy the projectile
    28.     Destroy(gameObject);
    29. }
    30.  
    Thanks in advance.
     
  2. waltermana

    waltermana

    Joined:
    Jun 8, 2010
    Posts:
    172
    you must assign instantiate to a temp variable and use that to do the rest of your work - instantiate copies w/e the 1st field you give it and returns that copy
     
  3. Artimese

    Artimese

    Joined:
    Nov 22, 2009
    Posts:
    794
    Yeah i used this line
    Code (csharp):
    1.  Instantiate(hitParticles);
    in the Fire function, and thats when I started getting the error, am I using instantiate correctly? I took a look at the rocket launcher script too, and it used instantiate too, but im not sure why it doesnt work with my gun script.
     
  4. Artimese

    Artimese

    Joined:
    Nov 22, 2009
    Posts:
    794
    Anyone know what im doing wrong?
     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
  6. Artimese

    Artimese

    Joined:
    Nov 22, 2009
    Posts:
    794
    Yeah I read that documentation, but the problem is my gun does not use a projectile it uses raycasting (like the machine gun in the fps tutorial).

    So clone = Instantiate(projectile, transform.position, transform.rotation); doesnt seem to really work because it uses position, rotation, and transform, but all im using is raycast, so how do I implement instantiate using raycasts?
     
  7. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html

    http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html

    http://unity3d.com/support/documentation/ScriptReference/RaycastHit-point.html

     
  8. Artimese

    Artimese

    Joined:
    Nov 22, 2009
    Posts:
    794
    Yeah I have all that stuff implemented into my script, but for some reason the Hitparticles doesnt appear...

    The only reason I decided to use raycast instead of projectiles is because when i shoot my gun, some times it shoots right thru a wall, sometimes it doesnt, If anyone knows a fix for that, then my problem would be solved.