- Home /
 
A few C# problems.
Below are two scripts, one for summoning a projectile (goes on the gun), and the other for collisions which goes on the projectile. The first problem is "Cannot cast from source type to destination type." While i have googled the problem none of the answers explain what is wrong but rather give a specific fix. Full debug.log
InvalidCastException: Cannot cast from source type to destination type. WeaponScript.ShootingFunc () (at Assets/Script/WeaponScript.cs:43) WeaponScript.Update () (at Assets/Script/WeaponScript.cs:23)
The second problem i'm having is that every time i fire the Ammo var is supposed to go down by one, however it does not. Also i have a simple delay between shots and this is also not working, is this due to the invalidCastException?
This is the code for the weapon and projectile instancing
 using UnityEngine;
 using System.Collections;
 
 public class WeaponScript : MonoBehaviour {
     public Rigidbody projectile;
     public int ammoLeft = 0;
     public bool isWeaponCycling;
     public float weaponCycling; 
 
     void Start () {
 
         isWeaponCycling = false;
         weaponCycling = 5.0f;
 
     }
 
     void Update () {
     if (Input.GetButtonDown ("Fire1")) 
         {
             if (isWeaponCycling == false)
             {
             ShootingFunc();
             }else
             {
             Debug.Log("Cannot Fire at this time");
             }
 
         }
         if (isWeaponCycling)
         {
             weaponCycling -= Time.deltaTime;
             if (weaponCycling < 0)
             {
                 isWeaponCycling = false;
                 weaponCycling = 5.0f;
             }
         }
     }
 
     void ShootingFunc ()
     {
         Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
         isWeaponCycling = true;
         ammoLeft -= 1;
         Debug.Log ("You have " + ammoLeft + " rounds left");
 
             if (ammoLeft == 0)
             {
             Debug.Log ("Horey shit guys no ammo left");
             }
         }
 }
 
               The other script goes on the actual projectile.
 public class ProjectileScript : MonoBehaviour {
     public int projectileSpeed = 500;
     public float timeTillDeath = 10;
 
 
         void FixedUpdate () {
                 rigidbody.AddForce (Vector3.forward * 10);
         }
 
     void Update () {
         transform.Translate (Vector3.forward * Time.deltaTime * 600);
         timeTillDeath = timeTillDeath - Time.deltaTime;
 
         if (timeTillDeath < 0) 
             {
             Destroy (gameObject);
             }
     }
 
     void OnCollisionEnter(Collision col){
         if (col.gameObject.CompareTag("Enemy")){
             col.gameObject.SendMessage("HealthAdj", -10, SendMessageOptions.DontRequireReceiver);
         }
         Destroy(gameObject); // bullet suicides after hitting anything
     }
 }
 
 
 
               Thanks in advance, this has been bugging me all morning.
I'm going to assume that
 InvalidCastException: Cannot cast from source type to destination type. WeaponScript.ShootingFunc () (at Assets/Script/WeaponScript.cs:43) 
 
                  is here:
 Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation);
 
                  You might want to do
 Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
 
                  if that doesnt work then you probably need to
 GameObject bullet = (GameObject)Instantiate(projectile, transform.position, transform.rotation);
 
                  and then use bullet.rigidbody if you need to mess with its rigidbody
Since your error happens prior to your ammo/time between shots it is likely it is ignoring the lines below.
$$anonymous$$y guess is you have a prefab you want to instantiate as the bullet. You should try changing your prefab from Rigidbody to GameObject.
 public GameObject projectile;
 
 GameObject bulletPfb = (GameObject)Instantiate(projectile, transform.position, transform.rotation);
 
                  When you want to access the rigidbody on that prefab use:
 Rigidbody bullet = bulletPfb.GetComponent(Rigidbody);
 
                 Answer by KpjComp · Oct 20, 2014 at 12:48 PM
Your trying to Instantiate a RigidBody, if your providing a location / rotation then Unity is expecting a GameObject.
Change -> public Rigidbody projectile; To -> public GameObject projectile;
And then drag the projectile prefab on to the projectile property.
Your answer
 
             Follow this Question
Related Questions
InvalidCastException: Cannot cast from source type to destination type. 3 Answers
InvalidCastException: Cannot cast from source type to destination type. ??? 2 Answers
"InvalidCastException" with MeshFilter from JS Array to builtin array. 2 Answers
"Cannot cast from source type to destination type"- instantiating 4 Answers
Deserializing problem at runtime, InvalidCastException 1 Answer