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 /
This question was closed Jul 19, 2013 at 10:19 PM by MadJohny for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by MadJohny · Jul 19, 2013 at 08:22 PM · c#errorerror-handling

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

}

Comment
Add comment · Show 11
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 clunk47 · Jul 19, 2013 at 08:30 PM 1
Share

Looks like u also have a script you're trying to reference called GJAPI. Post that too?

avatar image clunk47 · Jul 19, 2013 at 08:31 PM 1
Share

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.

avatar image clunk47 · Jul 19, 2013 at 08:33 PM 1
Share

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.

avatar image MadJohny · Jul 19, 2013 at 08:42 PM 1
Share

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

avatar image MadJohny · Jul 19, 2013 at 08:42 PM 0
Share

by the way the error happens when I press mouse (2) to use it, you must try the script in game

Show more comments

1 Reply

  • Sort: 
avatar image
2
Best Answer

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...

Comment
Add comment · Show 5 · 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
avatar image supercouge · Jul 19, 2013 at 10:05 PM 1
Share

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...

avatar image clunk47 · Jul 19, 2013 at 10:07 PM 1
Share

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.

avatar image MadJohny · Jul 19, 2013 at 10:19 PM 1
Share

Check question's comment, it's fixed, and it was actually changing from Transform to GameObject

avatar image clunk47 · Jul 19, 2013 at 10:21 PM 1
Share

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.

avatar image supercouge · Jul 19, 2013 at 10:32 PM 0
Share

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. :/

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Expression denotes a `method group', where a `variable', `value' or `type' was expected 1 Answer

Multiple Cars not working 1 Answer

Error, I have no idea why :( 2 Answers

Distribute terrain in zones 3 Answers

error CS0118: `New_Career.Fame' is a `field' but a `type' was expected 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