unity code is working fine .
but my code is running with an error of InvalidCastExecption.
my code is instantiating bullet object but dosent add force to it.
at prifix (GameObject) or at suffix as GameObject showing same error.
compiler error is at line 25.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shooting : MonoBehaviour {
/*unity code is working fine
* but my code is running with an error of InvalidCastExecption.
* my code is instantiating bullet object but dosent add force to it.
* at prifix (GameObject) or at suffix as GameObject showing same error
* compiler error is at line 25.
* */
//public Rigidbody bulletPrefab;//unit
public GameObject bulletPrefab;//my code
public Transform barrel; // unity and my common code
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1")) {
//Rigidbody bullet = (Rigidbody) Instantiate (bulletPrefab, barrel.position, barrel.rotation);//unity
GameObject bulletObj = (GameObject) Instantiate(bulletPrefab, barrel.position, barrel.rotation);//my code
//bullet.AddForce (barrel.right * 2000); //unity
Rigidbody bullet = bulletObj.GetComponent<Rigidbody>();//my code
bullet.AddForce (barrel.right * 2000);//code
}
}
}