Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with instantiating Projectile

Discussion in 'Scripting' started by Ricks, Jan 16, 2012.

  1. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    Hello all,
    I got a problem which I cannot seem to solve and I don't know why. In my script I try instantiate an arrow as follows:

    PHP:
    Vector3 arrowLookPos target.position-transform.position;
            
    Quaternion arrowLookRotation Quaternion.LookRotation(arrowLookPos);        
            
    Rigidbody clone = (RigidbodyInstantiate(arrowtransform.positionarrowLookRotation);
            clone.
    velocity = clone.transform.forward 30;
            
    Physics.IgnoreCollision(clone.collidertransform.root.collider);
    This code works correctly as long as I make a "public Rigidbody arrow" and Drag&Drop my Prefab (with the name "Arrow") into the specific slot. However if I try to instantiate using "Resources.Load("Arrow") I get an Exception like "Object Reference is not set to an instance of an object". This is the code I used:

    PHP:
    GameObject arrow = (GameObjectResources.Load("Arrow");
            
            
    Rigidbody clone = Instantiate(arrowtransform.positionarrowLookRotation) as Rigidbody;
            clone.
    velocity = clone.transform.forward 30;
    The compiler shows me the error in the last line (the one with clone.velocity). Therefore my question is: how can I instantiate rigidbody projectiles correctly?
     
    Last edited: Jan 16, 2012
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Since ive never used resources.load, this is a bit of a guess, but if its not in the correct path, it wont load.

    Assets/Resources/[prefab name]
     
  3. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    Thanks for the answer, but I got the path right. I have it in the "Resources" folder. Also I have another prefab in this folder which I instantiate (a plain gameobject, not a rigidbody) which loads correctly everytime without error. I really don't get where the error is with this rigidbody projectile...
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    why dont you just instantiate the gameobject then access the rigidbody?


    GameObject instance = Instantiate(Resources.Load("enemy", typeof(GameObject)));
    instance.rigidBody.velocity....
     
  5. avidesk

    avidesk

    Joined:
    Sep 12, 2011
    Posts:
    62
    Similar to what jlcnz is saying...

    If I understand correctly you should be instantiating a gameObject and modifying the gameObject's transform. I thought Rigidbody is simply a component that needs to have a gameObject as a parent? A rigidbody by itself doesn't technically have a transform component, hence the null reference exception.

    This is just my first thought, I could be totally wrong.
     
  6. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    Thanks again for this hint, I have already tried that, and it works on the first view - until I saw that:
    Every instance spawns at first for a very short time on (0,0,0) in the scene and only afterwards you can set the correct location. So if you spawn multiple instances you see lots of flickering disappering/appearing arrows in the scenery before the arrows are actually set to their specific location :-|

    Unfortunately there is no function which allows me to do that: Rigidbody = Instantiate(Resources.Load("enemy", typeof(GameObject),position,rotation). I mean, do I really need to uplift the whole scenery to have (0,0,0) out of sight? There should be another clean solution, ain't it? :-(
     
  7. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2. GameObject projectile = Instantiate(Resources.Load("arrow", typeof(GameObject)), location, rotation);
    3.  
    4. projectile.rigidBody....
    5.  
    6.  
    if if doesnt like that, try
    GameObject projectile = Instantiate((GameObject)Resources.Load("arrow", typeof(GameObject)), location, rotation);
     
  8. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    THANKS a million! This was it! It works!

    (I always tried to instantiate it with "Rigidbody clone = ...Instantiate/Resources.Load etc." or to cast the GameObject to a "Rigidbody" which resulted in errors. Obviously I have understood this variable "Rigidbody" wrong. I only need to instantiate the GameObject and then access its rigidbody component).
     
    Last edited: Jan 16, 2012