Rigidbody instatiating in C# problem

Discussion in 'Scripting' started by Melly Andrew, Jan 18, 2011.

  1. Melly Andrew

    Melly Andrew

    New Member

    Joined:
    Jan 18, 2011
    Messages:
    40
    What do you think is wrong here in my code? Cause I got this error: "Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Rigidbody"

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BasicWeapon: MonoBehaviour {
    6.      public Transform throwProjectile;
    7.          public int projectileSpeed = 10000;            
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.    
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.         if(Input.GetButtonUp("Fire1")) {
    17.              Rigidbody newProjectile;
    18.              newProjectile = Instantiate(throwProjectile,
    19.                                              transform.position,
    20.                                              transform.rotation);
    21.              newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    22.         }      
    23.     }
    24. }
    25.  
    26.  
  2. celinscak

    celinscak

    New Member

    Joined:
    May 4, 2010
    Messages:
    102
    I'm not a C# programmer, but it seems to me there is no rigidbody attached to projectile prefab.
  3. Melly Andrew

    Melly Andrew

    New Member

    Joined:
    Jan 18, 2011
    Messages:
    40
    What do you mean? You mean the throwProjectile variable? Ive changed it to Rigidbody and its still the same :(
  4. celinscak

    celinscak

    New Member

    Joined:
    May 4, 2010
    Messages:
    102
    Click on the projectile prefab and then go Component -> Physics -> Rigidbody.
  5. Jesse Anders

    Jesse Anders

    New Member

    Joined:
    Apr 5, 2008
    Messages:
    2,857
    I don't think the problem is related to whether there is or is not a rigid body component attached. Rather, it looks like the problem is that the OP is trying to assign the return value of Instantiate(), which is definitely not of type Rigidbody in this case (it looks like it's of type UnityEngine.Object), to a variable of type Rigidbody.

    @The OP: Also, in this line:

    Code (csharp):
    1. newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    You're trying to access the 'rigidbody' property of the Rigidbody class, but this property doesn't exist.
  6. Mike L

    Mike L

    New Member

    Joined:
    Sep 14, 2010
    Messages:
    1,035
    I have a similar problem with this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Bow : MonoBehaviour {
    5.     private GameObject readyArrow;
    6.     public float initialSpeed = 0.0f;
    7.     public GameObject arrow;
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.         if (Input.GetButton("Fire1")) {
    12.             DrawBack();
    13.            
    14.             if (initialSpeed <= 65) {
    15.                 initialSpeed += 2;
    16.             }
    17.         }
    18.        
    19.         if (Input.GetButtonUp("Fire1")) {
    20.             Fire();
    21.         }
    22.     }
    23.    
    24.     public void DrawBack () {
    25.         if (!readyArrow) {
    26.             initialSpeed = 0;
    27.             readyArrow = Instantiate(arrow, transform.position, transform.rotation);
    28.            
    29.             if (!readyArrow.rigidbody) {
    30.                 readyArrow.AddComponent("Rigidbody");
    31.             }
    32.         }
    33.     }
    34.  
    35.  
    36.     public void Fire () {
    37.         if (readyArrow) {
    38.             readyArrow.rigidbody.velocity = transform.TransformDirection(0, initialSpeed, 0);
    39.             readyArrow.rigidbody.useGravity = true;
    40.             readyArrow = null;
    41.        
    42.         }
    43.     }
    44. }
    except its a GameObject to Object error
  7. Melly Andrew

    Melly Andrew

    New Member

    Joined:
    Jan 18, 2011
    Messages:
    40
    The thing is it's all working in JS, but I like C# better.
  8. elveatles

    elveatles

    New Member

    Joined:
    May 2, 2009
    Messages:
    147
    newProjectile should be a GameObject type. Instantiate returns an Object. You can cast by typing (GameObject)Instantiate(throwProjectile, transform.position, transform.rotation); So, basically what Jesse Sanders said, but with short example code.
    Last edited: Jan 18, 2011
  9. Aerozo

    Aerozo

    Member

    Joined:
    Sep 3, 2010
    Messages:
    40
    Here, this should work:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BasicWeapon: MonoBehaviour {
    5.      public Transform throwProjectile;
    6.          public int projectileSpeed = 10000;            
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if(Input.GetButtonUp("Fire1")) {
    16.              GameObject newProjectile = Instantiate(throwProjectile,
    17.                                              transform.position,
    18.                                              transform.rotation) as GameObject;
    19.              newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    20.         }      
    21.     }
    22. }
    Last edited: Jan 18, 2011
  10. Melly Andrew

    Melly Andrew

    New Member

    Joined:
    Jan 18, 2011
    Messages:
    40
    Thanks that worked great, but there is one more problem on this line:

    Code (csharp):
    1. newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    Error: "Object reference not set to an instance of an object"
  11. Melly Andrew

    Melly Andrew

    New Member

    Joined:
    Jan 18, 2011
    Messages:
    40
    Can anyone help?
  12. Antitheory

    Antitheory

    New Member

    Joined:
    Nov 14, 2010
    Messages:
    549
    Maybe the problem is you are trying to use the rigidBody without creating it first. It could be... I would just add a rigidBody to the prefab, then you know it's there when you instantiate one.
  13. Redova

    Redova

    Guest

    Joined:
    Feb 13, 2011
    Messages:
    4
    I came across this threat googling this issue so ...

    Following the tutorial down below I came across the same problem as described above and following the solutions offered here I got the same errors as Melly Andrew. Again, Javascript works fine but C# doesn't.

    Tutorial: http://www.youtube.com/watch?v=wfpZ7_aFoko&feature=channel

    My prefab has a rigidbody and my update function looks like:
    Code (csharp):
    1.  
    2.     public Transform bulletPrefab;
    3.  
    4.     void Update () {
    5.         // Move the controler
    6.         CharacterController myControler = GetComponent<CharacterController>();
    7.         myControler.SimpleMove(transform.TransformDirection(Vector3.forward) * (Input.GetAxis("Vertical") * this.Speed));
    8.        
    9.         // Rotate the object
    10.         transform.Rotate(0.0f, Input.GetAxis("Horizontal") * this.Rotate, 0.0f);
    11.        
    12.         // Fire a bullet
    13.         if(Input.GetButtonDown("Jump"))
    14.         {
    15.             GameObject bullet;
    16.             bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as GameObject;
    17.             bullet.rigidbody.AddForce(transform.forward * 200);
    18.         }
    19.        
    20.     }
    21.  
    My work around is to add a separate script to the prefab and add force to it in the 'start' function of that script. If someone knows how to make it work without the extra script I would be interested
    Last edited: Feb 13, 2011
  14. pixelminer

    pixelminer

    New Member

    Joined:
    Jul 24, 2011
    Messages:
    2
    When scripting in C#, declaring an instance of the bullet as a "GameObject" will not work but declaring it as "Rigidbody" will. So, the above code should be changed to:

    Code (csharp):
    1.  
    2. // Fire a bullet
    3. if (Input.GetButtonDown("Jump"))
    4. {
    5.       Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as Rigidbody;
    6.       bullet.rigidbody.AddForce(transform.forward * 200);
    7. }
    8.  
    Be sure to make sure that the declaration for bulletPrefab uses "Rigidbody" instead of "Transform" like so:
    Code (csharp):
    1.  
    2. public Rigidbody bulletPrefab;
    3.  
    Also, you'll want to rotate your bullet before you fire it so the code to instantiate the bullet should really be:
    Code (csharp):
    1.  
    2. Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation) as Rigidbody;
    3.  
    Last edited: Aug 3, 2011
  15. Bharat5483

    Bharat5483

    New Member

    Joined:
    Aug 15, 2013
    Messages:
    1
    // check this line
    Rigidbody newProjectile;

    newProjectile = Instantiate(throwProjectile,

    transform.position,

    transform.rotation) as Rigidbody; // add as Rigidbody to create instantiate object
  16. Ginebro

    Ginebro

    New Member

    Joined:
    Aug 8, 2013
    Messages:
    17
    The line:
    Code (csharp):
    1.  
    2.             newProjectile = Instantiate(throwProjectile,
    3.                                              transform.position,
    4.                                              transform.rotation);
    5.  
    6.  
    Should be
    Code (csharp):
    1.  
    2.             newProjectile = Instantiate(throwProjectile,
    3.                                              transform.position,
    4.                                              transform.rotation) as Rigidbody;
    5.  
    6.  
  17. Dantus

    Dantus

    Member

    Joined:
    Oct 21, 2009
    Messages:
    3,801
    You guys just replied to a thread that is two years old and the answer was already there...