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 tomtomssi · Feb 17, 2014 at 09:32 AM · errormovementrigidbodyrigidbody2dcasting

InvalidCastException: Cannot convert from source type to destination type using a Rigidbody2D

I'm trying to move the clones but for some reason I'm getting this error when I'm trying to convert the instantiated bullet clone into a Rigidbody2D. How should I go about fixing this?

 using UnityEngine;
 using System.Collections;
 
 public class Shooting : MonoBehaviour {
     public Rigidbody2D bullets;
     public int speed;
     Rigidbody2D bulletClone;
     Vector2 playerPos;
     // Use this for initialization
     void Start () {
         speed = 10;
     }
     
     // Update is called once per frame
     void Update () {
         playerPos = GameObject.Find ("Player").transform.position;
         if(Input.GetButtonDown("Fire1")){
             bulletClone = (Rigidbody2D)Instantiate (bullets, transform.position, Quaternion.identity);
 
 
         }
     }
 }

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 emalb · Feb 17, 2014 at 01:54 PM 0
Share

Does it work if you do it like this?

bulletClone = Instantiate (bullets, transform.position, Quaternion.identity) as RigidBody2D;

2 Replies

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

Answer by wibble82 · Feb 17, 2014 at 02:07 PM

Hi

I don't think you can just instantiate a 'rigidbody2d', as the rigidbody2d is a component that is part of a game object. You need to create a prefab for your bullet, then instantiate that. So the a basic way of doing it might be:

  1. In the editor, create a bullet how you want it - a game object with its mesh, a rigid body 2d and whatever other parts you need

  2. Create a new prefab

  3. Drag the bullet onto the prefab (to turn the blank prefab into one that represents your bullet type). You can now delete the bullet itself if you don't want it in the world any more.

  4. Add a member to your script which is public with the code 'public GameObject bullet_prefab'

  5. Now in the editor when you select the shooting object you will see a property called 'bullet prefab'. Drag your bullet prefab into that property

  6. In your code you can now do 'GameObject newbullet = (GameObject)Instantiate(bullet_prefab)'

The new bullet object will be a full game object that now exists in the world that has a rigid body which you can access via its rigidbody2D property if you need to.

(correction) According to the docs it looks like you should be able to just instantiate the rigid body by itself. However instantiate would still return a GameObject with a rigid body attached so you'd still need to get the game object and then access its rigidbody2D member.

-Chris

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
avatar image
1

Answer by nesis · Feb 17, 2014 at 02:03 PM

Cast to GameObject, then get the rigidbody2D member:

 bulletClone = ((GameObject)Instantiate(bullets,transform.position,Quaternion.identity)).rigidbody2D;
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

21 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 avatar image avatar image avatar image

Related Questions

Help with translation and rotation on a rigid body 2D 0 Answers

Rigidbody.AddForce not being detected 2 Answers

Player movement not working with unity 5 1 Answer

2D Game, Character Slides off Slopes, Need to be Able to Walk on Them 1 Answer

Rigidbody makes object "fly" when moving up mountain 0 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