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 FreeTimeDev · Apr 12, 2011 at 10:29 AM · rigidbodyinstantiateinvalidcastexception

Invalid Cast Exception

Looking at the related tags I've had no luck getting this instantiate code fixed.

var projectile : Rigidbody;

and at some point, the Shoot function gets called.

    function Shoot()
{
    var instantiatedProjectile : Rigidbody;
    instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation );
    instantiatedProjectile.velocity = transform.TransformDirection ( Vector3( 0,0, 18));
    Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    lastShot = Time.time;
    timesShot +=1;
}

According to unity, the problem is in this line:

    instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation );

projectile and instantiateProjectile are both Rigidbody. There shouldn't be anything to change here. What am I missing?

Worth noting: I copy and pasted the instantiate code from another script and both the code, the prefab (projectile) and the world is nice and dandy. For some reason when I'm trying to reuse the code/prefab I'm getting this.

And the Prefab shooting the projectile also destroys itself with the shots. Even though I'm reusing the IgnoreCollision line.

Comment
Add comment
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

1 Reply

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

Answer by CHPedersen · Apr 12, 2011 at 10:44 AM

This is a shot in the dark, but did you make sure to drag'n'drop the prefab onto the variable "projectile" in the editor? If not, Unity won't know which prefab to instantiate a copy of at runtime.

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 FreeTimeDev · Apr 12, 2011 at 12:08 PM 0
Share

Oh sweet Jesus. This was it, but I swear I've checked it before. It wouldn't have been the first time it happened to me. I had "ShotBaddieShot" (the right one) in the editor but it had the Transform Icon, not the Rigidbody. I swear I replaced it a few times trying to get it to work. Ugh, I got back to my comptuer and you made it work. Thank you.

avatar image CHPedersen · Apr 12, 2011 at 01:00 PM 0
Share

You're most welcome. :) Happy it helped.

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

No one has followed this question yet.

Related Questions

Help with firing projectiles out of turret 1 Answer

Problem with colliding instances of same object 1 Answer

Instantiate rigidbody C# 1 Answer

Can't Spawn Rigidbody over network with NetworkServer.Spawn(); 1 Answer

Checking if object intersects? 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