Unity Community


Results 1 to 8 of 8

  1. Posts
    452

    Problem with instantiating Projectile

    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 Code:
    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 Code:
    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 by marctro; 01-16-2012 at 02:20 PM.


  2. Location
    NZ
    Posts
    2,933
    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]
    www.fingerbait.com :: Inferno - Multiplayer SciFi RTS :: Free Console

    Don't mind me, I'm just a Negative Nancy.


  3. Posts
    452
    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. Location
    NZ
    Posts
    2,933
    why dont you just instantiate the gameobject then access the rigidbody?


    GameObject instance = Instantiate(Resources.Load("enemy", typeof(GameObject)));
    instance.rigidBody.velocity....
    www.fingerbait.com :: Inferno - Multiplayer SciFi RTS :: Free Console

    Don't mind me, I'm just a Negative Nancy.


  5. Posts
    59
    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. Posts
    452
    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. Location
    NZ
    Posts
    2,933
    Code:  
    1. GameObject projectile = Instantiate(Resources.Load("arrow", typeof(GameObject)), location, rotation);
    2.  
    3. projectile.rigidBody....

    if if doesnt like that, try
    GameObject projectile = Instantiate((GameObject)Resources.Load("arrow", typeof(GameObject)), location, rotation);
    www.fingerbait.com :: Inferno - Multiplayer SciFi RTS :: Free Console

    Don't mind me, I'm just a Negative Nancy.


  8. Posts
    452
    Quote Originally Posted by jlcnz View Post
    GameObject projectile = Instantiate((GameObject)Resources.Load("arrow", typeof(GameObject)), location, rotation);
    projectile.rigidBody....
    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 by marctro; 01-16-2012 at 03:10 PM.

Posting Permissions

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