Unity Community


Results 1 to 17 of 17

  1. Posts
    40

    Rigidbody instatiating in C# problem

    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:  
    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.              Rigidbody newProjectile;
    17.              newProjectile = Instantiate(throwProjectile,
    18.                                              transform.position,
    19.                                              transform.rotation);
    20.              newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    21.         }      
    22.     }
    23. }


  2. Location
    Croatia
    Posts
    102
    I'm not a C# programmer, but it seems to me there is no rigidbody attached to projectile prefab.


  3. Posts
    40
    What do you mean? You mean the throwProjectile variable? Ive changed it to Rigidbody and its still the same


  4. Location
    Croatia
    Posts
    102
    Click on the projectile prefab and then go Component -> Physics -> Rigidbody.


  5. Posts
    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:  
    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. Location
    Somewhere on this place we know as earth
    Posts
    1,035
    I have a similar problem with this:
    Code:  
    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
    Currently working on : Paraverse

    On the Backburner : Operation Snowball, Legends of Merlin


  7. Posts
    40
    The thing is it's all working in JS, but I like C# better.


  8. Posts
    147
    Quote Originally Posted by Melly Andrew View Post
    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:  
    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.              Rigidbody newProjectile;
    17.              newProjectile = Instantiate(throwProjectile,
    18.                                              transform.position,
    19.                                              transform.rotation);
    20.              newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
    21.         }      
    22.     }
    23. }
    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 by elveatles; 01-18-2011 at 01:32 PM.


  9. Location
    UK
    Posts
    40
    Here, this should work:

    Code:  
    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 by Aerozo; 01-18-2011 at 03:23 PM. Reason: Forgot code tags


  10. Posts
    40
    Thanks that worked great, but there is one more problem on this line:

    Code:  
    1. newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);

    Error: "Object reference not set to an instance of an object"


  11. Posts
    40
    Can anyone help?


  12. Location
    Halifax, Canada
    Posts
    549
    Quote Originally Posted by Melly Andrew View Post
    Thanks that worked great, but there is one more problem on this line:

    Code:  
    1. newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);

    Error: "Object reference not set to an instance of an object"
    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. Posts
    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...eature=channel

    My prefab has a rigidbody and my update function looks like:
    Code:  
    1.     public Transform bulletPrefab;
    2.  
    3.     void Update () {
    4.         // Move the controler
    5.         CharacterController myControler = GetComponent<CharacterController>();
    6.         myControler.SimpleMove(transform.TransformDirection(Vector3.forward) * (Input.GetAxis("Vertical") * this.Speed));
    7.        
    8.         // Rotate the object
    9.         transform.Rotate(0.0f, Input.GetAxis("Horizontal") * this.Rotate, 0.0f);
    10.        
    11.         // Fire a bullet
    12.         if(Input.GetButtonDown("Jump"))
    13.         {
    14.             GameObject bullet;
    15.             bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as GameObject;
    16.             bullet.rigidbody.AddForce(transform.forward * 200);
    17.         }
    18.        
    19.     }

    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 by Redova; 02-13-2011 at 08:11 AM.


  14. Posts
    2
    Quote Originally Posted by Redova View Post
    Code:  
    1. // Fire a bullet
    2. if (Input.GetButtonDown("Jump"))
    3. {
    4.       GameObject bullet;
    5.       bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as GameObject;
    6.       bullet.rigidbody.AddForce(transform.forward * 200);
    7.         }
    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:  
    1. // Fire a bullet
    2. if (Input.GetButtonDown("Jump"))
    3. {
    4.       Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as Rigidbody;
    5.       bullet.rigidbody.AddForce(transform.forward * 200);
    6. }

    Be sure to make sure that the declaration for bulletPrefab uses "Rigidbody" instead of "Transform" like so:
    Code:  
    1. public Rigidbody bulletPrefab;

    Also, you'll want to rotate your bullet before you fire it so the code to instantiate the bullet should really be:
    Code:  
    1. Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation) as Rigidbody;
    Last edited by pixelminer; 08-03-2011 at 09:12 AM.


  15. Posts
    1
    // check this line
    Rigidbody newProjectile;

    newProjectile = Instantiate(throwProjectile,

    transform.position,

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


  16. Posts
    17
    The line:
    Code:  
    1.            newProjectile = Instantiate(throwProjectile,
    2.                                              transform.position,
    3.                                              transform.rotation);
    Should be
    Code:  
    1.            newProjectile = Instantiate(throwProjectile,
    2.                                              transform.position,
    3.                                              transform.rotation) as Rigidbody;


  17. Posts
    2,879
    You guys just replied to a thread that is two years old and the answer was already there...

Posting Permissions

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