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 YellowQuiet · May 07, 2014 at 05:21 AM · unity 2dconvertaccessing scriptsrigidbody 2d

Convert 3D object into 2D object via script

Hello,

I am trying to create a capsule at run time in my script that will shoot off of my player. However all of my players are 2D sprites, and the capsule is a 3D object. When I remove the 3D components in the script it works fine, but adding the 2D components wont work. I tried googling the issue but I could not find what was wrong with my script. I have tried adding 2D components onto a 3D object via the inspector in unity and that works. I am not sure what the issue is.

Thanks for any help in advanced!

 GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
 capsule.transform.localScale = new Vector3 (.1f, .1f, .1f); // need to make or bullet not gigantic
 
             if(tag % 2 == 0)
             {
                 Blue = GameObject.FindWithTag (tag.ToString ());
                 capsule.transform.position = Blue.transform.position;
 
             }
 
             else
             {
                 Red = GameObject.FindWithTag (tag.ToString ());
                 capsule.transform.position = Red.transform.position;
             }
 
             Vector2 movement = new Vector2 (-1, 0);
 
 //need to convert capsule into a 2d object so that it will interact with the rest of the game
 
             //we need to convert the capsule to a 2d object so that it will interact with the rest of our game
             
             //UnityEngine.Object.Destroy(capsule.GetComponent<Rigidbody>());
             //UnityEngine.Object.Destroy(capsule.GetComponent<CapsuleCollider>());
             
             
             //capsule.AddComponent<Rigidbody2D>();
             //capsule.AddComponent<BoxCollider2D>();
             //capsule.rigidbody2D.velocity = movement * speed;
 
 
         }
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
0

Answer by robertbu · May 07, 2014 at 06:55 AM

This looks like a Unity bug to me. The capsule you create using CreatePrimitive() does not have a CapsuleCollider nor a Rigidbody. Yet when I try to add a BoxCollider2D, I get the error:

Can't add component 'BoxCollider2D' to Capsule because it conflicts with the existing 'CapsuleCollider' derived component! UnityEngine.GameObject:AddComponent() Bug25b:Start() (at Assets/_Project/Scripts/Bug25b.cs:27)

When posting questions, the OP (original poster) often asserts that he believes his problems are due to bugs in Unity. Of all the 1000's of questions I've worked on, this is the first time I believe it to be true (and you missed your chance to correctly assert that it's all Unity's fault ;).

A workaround is to crate a prefab. Start with a Capsule game object, delete the collider, Add your Rigidbody2D and BoxCollider2D, make it a prefab, then use Instantiate() to create it at runtime.

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 YellowQuiet · May 07, 2014 at 09:43 AM 0
Share

I thought it was a bug but did not want to say so because i'm still new to unity. Thanks for the help!

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

20 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

Related Questions

get world position when click on minimap in unity 2d ? 0 Answers

Do Canvas Objects behave similarly under the effect of Rigidbody2D when it comes to position checking? 0 Answers

Unity 2D: sprite's rigidbody weight/rotation 0 Answers

How to convert unity2D mobile game to HTML5 mobile game? 0 Answers

Unity 2d - Rigidbody rotation constraints? 4 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