Unity Community


Results 1 to 8 of 8

  1. Location
    CA.
    Posts
    794

    Prefab Instantiate is null. Spawning Prefabs?

    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:  
    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:  
    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.     if (emitter)
    21.     emitter.emit = false;
    22.  
    23.     // Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
    24.     transform.DetachChildren();
    25.        
    26.     // Destroy the projectile
    27.     Destroy(gameObject);
    28. }

    Thanks in advance.


  2. Location
    Atlanta
    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. Location
    CA.
    Posts
    794
    Yeah i used this line
    Code:  
    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. Location
    CA.
    Posts
    794
    Anyone know what im doing wrong?


  5. Location
    Lognes, France
    Posts
    1,149
    Quote Originally Posted by waltermana
    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
    http://unity3d.com/support/documenta...stantiate.html

    clone = Instantiate(projectile, transform.position, transform.rotation);
    A lazy big fat chicken hibernating, sleeping peacefully.
    !


  6. Location
    CA.
    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. Location
    Lognes, France
    Posts
    1,149
    http://unity3d.com/support/documenta...s.Raycast.html

    static function Raycast (origin : Vector3, direction : Vector3, out hitInfo : RaycastHit, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool
    http://unity3d.com/support/documenta...aycastHit.html

    point - The impact point in world space where the ray hit the collider.
    http://unity3d.com/support/documenta...Hit-point.html

    RaycastHit.point
    var point : Vector3
    Description

    The impact point in world space where the ray hit the collider.
    A lazy big fat chicken hibernating, sleeping peacefully.
    !


  8. Location
    CA.
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •