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 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.
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.
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);
}
}
}
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.
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;