InvalidCastException: Cannot cast from source type to destination type.
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
}
}
}
Strange, I pasted that code into my own test project and had no issues, ran fine, instantiated and shot off my sphere prefab. Not sure why you're getting an error.
for what type of conceptual mistake give this error //Cannot cast from source type to destination type..
I'm sorry I don't understand that statement. I've taken your script, and removed the commented out bits for clarity. This is the result.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class shooting : $$anonymous$$onoBehaviour
{
public GameObject bulletPrefab;
public Transform barrel;
void Update()
{
if( Input.GetButtonDown( "Fire1" ) )
{
GameObject bulletObj = (GameObject)Instantiate( bulletPrefab, barrel.position, barrel.rotation );
Rigidbody bullet = bulletObj.GetComponent<Rigidbody>();
bullet.AddForce( barrel.right * 2000 );
}
}
}
With this script as long as I provide a valid bullet that has a rigidbody component, and a barrel transform, I can enter play mode and shoot without any casting exceptions.
Answer by prakharexjailia · Mar 03, 2017 at 02:54 PM
If there is no rigidbody component in my prefab then unity code will also not work. But unity code is working. Last in what conditions this error will generate. Exact Meaning of this cast exception error.,If there is no rigidbody component in my prefab unity code will also not work. But unity code is working. Last in what conditions this error will generate. Exact Meaning of this cast exception error.
Your answer
Follow this Question
Related Questions
InvalidCastException: Cannot cast from source type to destination type. 1 Answer
Missing the class attribute 'ExtensionOfNativeClass' 4 Answers
Error in Inventory Script 1 Answer
The name does not exist in the current context 3 Answers
InvalidOperationException: Collection was modified; enumeration operation may not execute. 0 Answers