- Home /
The question is answered, right answer was accepted
InvalidCastException: Cannot cast from source type to destination type.
I'm getting this error in my script: InvalidCastException: Cannot cast from source type to destination type. Player.HandOfCROS () (at Assets/Scripts/Player/Player.cs:47) Player.Update () (at Assets/Scripts/Player/Player.cs:26)
can you tell me how to fix it? my script:
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public Transform projectile;
public float temp;
public int health = 100;
public float fireSpeed = 1f;
[HideInInspector]
public float waitTilNextFire = 0f;
public GameObject[] enemies;
public GameObject[] thunders;
public float energy = 0;
public Transform smite;
void Update () {
if(energy >= 100){
energy = 100;
}
if(Input.GetMouseButtonDown(2) && energy == 100){
HandOfCROS();
energy = 0;
GJAPI.GetTrophy(2811);
}
if (Input.GetMouseButtonDown(0)){
if(waitTilNextFire <= 0){
Transform clone = (Transform) Instantiate (projectile, transform.position, transform.rotation);
waitTilNextFire = 1;
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
enemies = GameObject.FindGameObjectsWithTag("Enemy");
}
void HandOfCROS () {
health -= 40;
foreach(GameObject obj in enemies){
Transform _t = obj.transform;
//then create
Transform clone = (Transform)Instantiate(smite,_t.position,_t.rotation);
clone.parent = _t;
}
}
}
Looks like u also have a script you're trying to reference called GJAPI. Post that too?
Because that's the only error I'm getting. "GJAPI does not exist" Because I don't have a GJAPI script. When I get rid of that line, no other errors with this script.
I am guessing you have some static variable in some other script. Try posting all the code you are using that communicates with this code.
GJApi is from game jolt so I can have trophies using gamejolt's API, the problem certainly doesn't come from it, I already had it before having that line
by the way the error happens when I press mouse (2) to use it, you must try the script in game
Answer by clunk47 · Jul 19, 2013 at 09:57 PM
I didn't see the very last line there. Try changing
Transform clone = (Transform)Instantiate(smite,_t.position,_t.rotation); clone.parent = _t;
TO
GameObject clone = (GameObject)Instantiate(smite,_t.position,_t.rotation); clone.transform.parent = _t;
Then instead of clone.position, or anything else to do w/ its transform component, use clone.transform.position...
I don't think there is a problem with $$anonymous$$adJohny's line. Everything seems fine to me. However, according to the error, it is in this function that the problem is...
Invalid cast. Apparently on the last line. 47. Didn't see it before because it wasn't in the code area and I overlooked it.
Check question's comment, it's fixed, and it was actually changing from Transform to GameObject
Thank you sir for the vote up and accepted answer. I realized after I posted this that you had it figured out, so +1 to you as well.
It's weird, I tried on my side, with a code like yours :
foreach(GameObject go in cubes) {
Transform t = go.transform;
Transform clone = (Transform) Instantiate(cube, t.position, t.rotation);
clone.parent = t;
}
And it worked just fine. :/