Search Unity

How to distinguish between multiple components of same type (C#)

Discussion in 'Scripting' started by Mazgas, Apr 15, 2017.

  1. Mazgas

    Mazgas

    Joined:
    Dec 12, 2015
    Posts:
    10
    I was trying to use (GetComponent) in C# but my game object has 2 components from the same type (Wheel Joint 2D) so please how can distinguish between them?
    i can’t use the drag and drop thing because i use instantiate function.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    1) why do you have 2 WheelJoints on the same gameobject?

    2) in what manner do you want to tell them apart?
     
  3. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    kek0, Sab_Rango, Adil192 and 2 others like this.
  4. Mazgas

    Mazgas

    Joined:
    Dec 12, 2015
    Posts:
    10
    because i have to wheels on the car?
     
  5. Mazgas

    Mazgas

    Joined:
    Dec 12, 2015
    Posts:
    10
  6. Mazgas

    Mazgas

    Joined:
    Dec 12, 2015
    Posts:
    10
    thanks it worked. I use 2 because i have 2 wheels, is there something wrong with that?
     
    diegofcuendias likes this.
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    Usually having more than 1 component of the same type on a GameObject isn't so common. There are exceptions to this of course, so it's not a hardfast rule... it's just a general thing. And when I hear of more than one component of a type on the same GameObject, my first question is "why?" If there's no good reason as to why... then it's probably not supposed to be like that.

    Take example of a WheelJoint and WheelJoint2D... imagine if you put the WheelJoint's on the wheels, as opposed to the body of the car.

    Now you have 1 joint per GameObject, and each wheel is easily identifiable by the GameObject they're on.

    Then on your body you'll probably have a script for driving the car. On it you'll probably have fields for 'DriveWheel', which references the wheel that actually drives the vehicle (front wheel, or rear wheel, drive), and then an array for all other wheels (maybe it has more than 2 axes, like a tractor trailer). If it's a 4 wheel drive vehicle (2 axes), you might have 2 fields for 'DriveWheel', and then array of wheels for non drive wheels.

    Furthermore, you never answered my question 2:
    My point being... what's the difference between the both wheels? Having an array with both wheels doesn't mean much of anything unless you know which is which, and WHY you must know which is which.

    Like... is this a front wheel drive car, so you must distinguish each wheel from each other so you know which one is the drive wheel, and which isn't? Ok... well, see my previous comments above about how that impacted my theoretical design. With just an array... how do you know which is the front wheels and which is the rear wheels? A rear wheel drive vehicle controls differently than front wheel drive. So that implication is sort of important.

    And usually you would tell by which wheel object each is connected to. That is the gameobject the component is on if you put the WheelJoint on the wheel itself... or the GameObject the WheelJoint points to if you had put them on the body.
     
    Last edited: Apr 15, 2017
    Bunny83 likes this.
  8. bz_tian

    bz_tian

    Joined:
    Jun 28, 2018
    Posts:
    4
    You shouldn't assume here. I am working on a 2d program that uses excel like fields, with a row containing multiple items, and I've decided to make rows GameObjects with it's fields components of those GameObjects (I can instantiate one row at a time as needed).. It isn't exactly excel, but the anology applies.. Then I only need to resize one object for instance. It's cleaner. I shouldn't have to explain this.

     
    kek0 and phobos2077 like this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    I didn't assume here.

    My entire point in that post is that more information was needed so one doesn't have to assume.

    And yes, you shouldn't have to explain this... no one asked you to dig up a thread over a year old to explain your edge case. An edge case I excepted for in my explanation by saying it's not a hard fast rule.
     
  10. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    Even for this excel-like thing, child gameObjects are better. Resizing the parent still works perfectly. It makes it simpler to re-arrange, test, add new parts later. It makes moving NxM chunks simpler. Without them you usually end up pulling out matching Components, maybe saving them somewhere else, then realizing that's exactly what gameObjects are for. They're just nodes, who doesn't like nodes?
     
  11. poopman300

    poopman300

    Joined:
    May 8, 2018
    Posts:
    2
    bruh, wheeljoint2d's don't work when theyre on Empties or the wheels o_O
     
    CrabRabbit and emobe_ like this.