Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Why do I get: "Cannot cast from source type to destination type"?

$
0
0
I'm working on a project where a cannon shoots a cannonball. I've manage to create a simple code for shooting the cannonball. It worked just fine. Then I fumbled on the code and as it didn't work I changed back to the original. And now I always get the message 'Cannot cast from source type to destination type'. Here is the script that handles the shooting: using UnityEngine; using System.Collections; public class shootCanonnball : MonoBehaviour { public Rigidbody cannonball; public Transform shotSpawn; public float shootForce; void Update () { if (Input.GetMouseButtonDown (0)) { GameObject cannonB = (GameObject)Instantiate(cannonball, shotSpawn.position, shotSpawn.rotation); cannonB.GetComponent().AddForce(transform.up * shootForce, ForceMode.Impulse); } } } I've looked up the other answers about this problem, but none of them work for me. It just seems like Unity made up an error and I don't know from where. I even created a new project with new objects in it and wrote the code again. Again, Same error on a different project.

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>