Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MobiusDev · Oct 20, 2014 at 12:34 PM · invalidcastexception

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wisearn · Oct 20, 2014 at 12:40 PM 1
Share

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

avatar image b1gry4n · Oct 20, 2014 at 12:42 PM 1
Share

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


1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges