Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiated object is always null

Discussion in 'Editor & General Support' started by m0rr0ws0n, Sep 27, 2014.

  1. m0rr0ws0n

    m0rr0ws0n

    Joined:
    Sep 18, 2014
    Posts:
    65
    Code below is for muzzle flash and firing a bullet but the instantiated object, which is a bullet, is always null. Check the fire function, what am I doing wrong besides calling Instantiate. =/

    Code (CSharp):
    1. public class muzzleflash : MonoBehaviour {
    2.    
    3.     public Light li;
    4.     public float maxTime = 0.5F;
    5.     public float currTime = 0.0F;
    6.     public float bulletSpeed;
    7.     public Transform bullet;
    8.     public Rigidbody clone;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         transform.renderer.enabled = false;
    13.         li.enabled = false;
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.  
    20.         currTime += Time.deltaTime;
    21.  
    22.         if(currTime < maxTime)
    23.         {
    24.  
    25.         if(Input.GetButtonDown("Fire1"))
    26.         {
    27.                 flip ();
    28.                 Invoke("flip", 0.02F);
    29.                 fire ();
    30.  
    31.  
    32.         }
    33.         }
    34.         else currTime = 0.0F;
    35.  
    36.  
    37.  
    38.     }
    39.  
    40.     void flip()
    41.     {
    42.         transform.renderer.enabled = !transform.renderer.enabled;
    43.         li.enabled = !li.enabled;
    44.     }
    45.  
    46.     void fire()
    47.     {
    48.         clone = Instantiate(bullet, transform.position, transform.rotation) as Rigidbody;
    49.         Debug.Log(clone);
    50.  
    51.     }
    52. }
    53.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Set bullet to Rigidbody instead of Transform.
    Afterwards, you need to re-assign it in the inspector otherwise it will have a type mismatch.
     
  3. m0rr0ws0n

    m0rr0ws0n

    Joined:
    Sep 18, 2014
    Posts:
    65
    not working. I'm trying to move it after its created . Thats when its telling me the clone object is null..but it spawns correctly.
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Does your gameobject actually have a rigidbody attached to it? The bullet prefab...
     
  5. m0rr0ws0n

    m0rr0ws0n

    Joined:
    Sep 18, 2014
    Posts:
    65
    yeah
     
  6. m0rr0ws0n

    m0rr0ws0n

    Joined:
    Sep 18, 2014
    Posts:
    65
    ok nevermind had to delete the prefab and make a new one for it to work.
     
  7. secondbreakfast

    secondbreakfast

    Joined:
    Jan 5, 2013
    Posts:
    98
    The 'as Rigidbody' in the instantiate line will return null if the instance isn't a Rigidbody. in this case it is a Transform because that is what you are giving it.