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”

using UnityEngine;
using System.Collections;

public class BasicWeapon: MonoBehaviour {
	 public Transform throwProjectile;
         public int projectileSpeed = 10000;         	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        if(Input.GetButtonUp("Fire1")) {
             Rigidbody newProjectile;
             newProjectile = Instantiate(throwProjectile,
                                             transform.position,
                                             transform.rotation);
             newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
        }		
	}
}

I’m not a C# programmer, but it seems to me there is no rigidbody attached to projectile prefab.

What do you mean? You mean the throwProjectile variable? Ive changed it to Rigidbody and its still the same :frowning:

Click on the projectile prefab and then go Component → Physics → Rigidbody.

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:

newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);

You’re trying to access the ‘rigidbody’ property of the Rigidbody class, but this property doesn’t exist.

I have a similar problem with this:

using UnityEngine;
using System.Collections;

public class Bow : MonoBehaviour {
	private GameObject readyArrow;
	public float initialSpeed = 0.0f;
	public GameObject arrow;
	
	// Update is called once per frame
	void Update () {
		if (Input.GetButton("Fire1")) {
			DrawBack();
			
			if (initialSpeed <= 65) {
				initialSpeed += 2;
			}
		}
		
		if (Input.GetButtonUp("Fire1")) {
			Fire();
		}
	}
	
	public void DrawBack () {
		if (!readyArrow) {
			initialSpeed = 0;
			readyArrow = Instantiate(arrow, transform.position, transform.rotation);
			
			if (!readyArrow.rigidbody) {
				readyArrow.AddComponent("Rigidbody");
			}
		}
	}


	public void Fire () {
		if (readyArrow) {
			readyArrow.rigidbody.velocity = transform.TransformDirection(0, initialSpeed, 0);
			readyArrow.rigidbody.useGravity = true;
			readyArrow = null;
		
		}
	}
}

except its a GameObject to Object error

The thing is it’s all working in JS, but I like C# better.

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.

Here, this should work:

using UnityEngine;
using System.Collections;

public class BasicWeapon: MonoBehaviour {
	 public Transform throwProjectile;
         public int projectileSpeed = 10000;         	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
        if(Input.GetButtonUp("Fire1")) {
             GameObject newProjectile = Instantiate(throwProjectile,
                                             transform.position,
                                             transform.rotation) as GameObject;
             newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);
        }		
	}
}

Thanks that worked great, but there is one more problem on this line:

newProjectile.rigidbody.AddForce(transform.right * projectileSpeed);

Error: “Object reference not set to an instance of an object”

Can anyone help?

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.

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:

	public Transform bulletPrefab;

	void Update () {
		// Move the controler
		CharacterController myControler = GetComponent<CharacterController>();
		myControler.SimpleMove(transform.TransformDirection(Vector3.forward) * (Input.GetAxis("Vertical") * this.Speed));
		
		// Rotate the object
		transform.Rotate(0.0f, Input.GetAxis("Horizontal") * this.Rotate, 0.0f);
		
		// Fire a bullet
		if(Input.GetButtonDown("Jump"))
		{
			GameObject bullet;
			bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as GameObject;
			bullet.rigidbody.AddForce(transform.forward * 200);
		}
		
	}

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

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:

// Fire a bullet
if (Input.GetButtonDown("Jump"))
{
      Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, Quaternion.identity) as Rigidbody;
      bullet.rigidbody.AddForce(transform.forward * 200);
}

Be sure to make sure that the declaration for bulletPrefab uses “Rigidbody” instead of “Transform” like so:

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:

Rigidbody bullet = Instantiate(bulletPrefab, GameObject.Find("SpawnPoint").transform.position, GameObject.Find("SpawnPoint").transform.rotation) as Rigidbody;

// check this line
Rigidbody newProjectile;

newProjectile = Instantiate(throwProjectile,

transform.position,

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

The line:

            newProjectile = Instantiate(throwProjectile,
                                             transform.position,
                                             transform.rotation);

Should be

            newProjectile = Instantiate(throwProjectile,
                                             transform.position,
                                             transform.rotation) as Rigidbody;

You guys just replied to a thread that is two years old and the answer was already there…