Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jul 19, 2016 at 02:16 PM by zoggzter for the following reason:

Question is off-topic or not relevant

avatar image
0
Question by zoggzter · Jul 18, 2016 at 08:30 AM · c#wheel

C# Get specific component of gameobject

I have multiple WheelJoint2D components on a gameobject. How would I go about removing a specific one of those WheelJoint2Ds?

Comment
Add comment · Show 2
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 ahaykal · Jul 18, 2016 at 08:41 AM 0
Share

What you could do is declare public variables :

public WheelJoint2D wj1; public WheelJoint2D wj2;

And then destroying the one you want.

avatar image zoggzter ahaykal · Jul 18, 2016 at 05:31 PM 0
Share

I have made an array of all the WheelJoint2Ds of the gameobject, what do I do now?

1 Reply

  • Sort: 
avatar image
0

Answer by jdean300 · Jul 18, 2016 at 06:08 PM

Unless you maintain a reference to each component such as:

 public class MyComponent : MonoBehaviour{
     public WheelJoint2D WheelJointOne; //You can set this from the inspector
     public WheelJoint2D WheelJointTwo; //You can set this from the inspector
 }

The best you can do is get all instances of that component and then filter the results based on known properties:

 public void SomeFunction(){
     WheelJoint2D[] wheels = GetComponents<WheelJoint2D>();
 
     foreach (WheelJoint2D w in wheels){
         //Find the joint that matches some parameters, then you can delete it.
     }
 }

The filtering method is not ideal because it can be pretty hard to differentiate between two instances of a component some times, but it is possible in some cases. Assigning references within the inspector using public variables is really the easiest way to get things right.

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 zoggzter · Jul 19, 2016 at 11:40 AM 0
Share

How exactly do I delete it? I have made an array that stores all the wheeljoints on the gameobject and called it "wheels" I made a loop and checked if it matched the one I was looking for, and called the int for it "i"

 Destroy (wheels[i]);

This results in the error: "Argument is out of range."

avatar image ahaykal zoggzter · Jul 19, 2016 at 11:43 AM 0
Share

Can you show us the loop script? Also what helps is if you print(wheels[i])

avatar image zoggzter ahaykal · Jul 19, 2016 at 02:04 PM 0
Share
 for (int i = 0; i < wheels.Count; i++){
     if (wheels[i].connectedBody == hit.transform.GetComponent<Rigidbody2D>()){
         Destroy (wheels[i]);
     }
 }

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I am trying to change forward friction stiffness with code. 0 Answers

Wheel rotation 1 Answer

I am a beginner and lost with error code CS00246 AND CS0101,CS111 2 Answers

My game script is fine but I'm still getting unexpected class error 0 Answers

Convert Keyboard controlls to UI Buttons 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