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 MonkeyAssassin8 · Dec 15, 2010 at 01:23 PM · typecastsourceinvalidcastexceptiondestination

"Cannot cast from source type to destination type"- instantiating

i've seen a few questions asked that are related to this, but none work, please help me figure out my problem, I have an AI that shoots bullets, but I get the error: "Cannot cast from source type to destination type".

var projectile : Rigidbody; var attackTimer : float = 1; var coolDown : float = 1;

function Shoot () {

 if(attackTimer > 0)
     attackTimer -= Time.deltaTime;

 if(attackTimer < 0)
     attackTimer = 0;

 if(attackTimer == 0){
     var clone : Rigidbody;
     //this is where it shows the error...
     clone = Instantiate(projectile, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection (Vector3.forward * 10);

     attackTimer = coolDown;
 }

}

Comment
Add comment · Show 1
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 Statement · Dec 15, 2010 at 02:15 PM 0
Share

I am not a JS coder so I dont know about all the subtle differences but can you try to cast the result from Instantiate to Rigidbody type? Such as (Rigidbody)Instantiate(pro....

4 Replies

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

Answer by MonkeyAssassin8 · Dec 16, 2010 at 12:50 PM

I figured it out and my problem was that I had another script which activated the shoot script, once I merged it into one script it worked perfectly

Comment
Add comment · Show 3 · 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 jister · Dec 27, 2011 at 06:31 PM 0
Share

sorry to bump this, but could be more clear on what was wrong?

avatar image MP0732 · Jun 27, 2012 at 07:30 PM 0
Share

I had the same problem. What I think he means to say is that you cannot have two different scripts instantiate the same prefab at the same time.

Once I copied the prefab and renamed and assigned the copy to the second script, both scripts worked fine.

Here's my own, second script, generalized, which was in this area of the script identical to the first:

var projectilePrefab : Rigidbody;

function ShootProjectile () { var newProjectile : Rigidbody = Instantiate(projectilePrefab, transform.position, transform.rotation); newProjectile.transform.Rotate(90, 0, 0); newProjectile.name = "projectile"; newProjectile.AddForce(newProjectile.transform.up * 500); Physics.IgnoreCollision(transform.root.collider, newProjectile.collider, true);

I simply assigned a copy of the original prefab to the variable "projectilePrefab", and the script worked fine.

avatar image Eric5h5 · Jun 27, 2012 at 07:35 PM 0
Share

you cannot have two different scripts instantiate the same prefab at the same time.

Actually there are no problems doing that.

avatar image
0

Answer by Patrick Lynch · Apr 24, 2011 at 04:57 AM

You've got to cast it to a transform, then you can get the gameObject or any other components.

Transform g = (Transform) Instantiate(indicatorPrefab, new Vector3(0,0,0), Quaternion.identity); GameObject obj = g.gameObject; GUITexture gui = g.gameObject.GetComponent();

Comment
Add comment · Show 2 · 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 defaxed · Jul 25, 2011 at 05:43 PM 0
Share

Thanks Patrick :)

avatar image KHIZAR HAYAT · Dec 31, 2013 at 11:12 AM 0
Share

thanx alot its working in my problem :D

avatar image
0

Answer by bateleur · Dec 15, 2010 at 02:05 PM

Assuming you're sure that 'projectile' is not null, it appears you might have encountered an instance of the problem discussed here:

http://forum.unity3d.com/threads/1726-Instantiate-and-type-cast-errors

If so, as it recommends, restarting Unity should fix it. (Where by "fix" I mean that it will stop happening for now but may recur later.)

Comment
Add comment · Show 1 · 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 Statement · Dec 15, 2010 at 02:13 PM 0
Share

That "fix" just sounds like you're recompiling the scripts. If the script had an error in the previous build and for some reason did not pick up the changes to the file (which happen frequently), the old build would still be in effect along with any errors they caused. There should be no need to restart unity but to simply reimport the script. I may of course be mistaken, but I find it highly reasonable this was the case.

avatar image
0

Answer by gw1108 · Jan 20, 2014 at 11:11 AM

Instantiate returns type of object which is the base class for all objects. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

I usually cast to game object then get the component, or in your case rigidbody, instead. However, to keep your code you would need to cast (Rigidbody)Instantiate().

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

4 People are following this question.

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

unable to cast from source to destination 0 Answers

Cannot cast from source to destination type 1 Answer

"Cannot cast from source type to destination type" 2 Answers


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