Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetComponent, singletons and static var

Discussion in 'Scripting' started by ninja_gear, Jan 14, 2015.

  1. ninja_gear

    ninja_gear

    Joined:
    Jul 31, 2013
    Posts:
    15
    I am making an rpg. I was moving fast through the camera and motor controls. Then I started working on the character command inputs.

    When I started on the targeting it seems like every object (player, character, target) are all passing
    around objects as arguments. Which I then have to constantly use GetComponents and .gameObject on because it's part transform and part character. My first question is how intense is GetComponents and should I minimize usage?

    So I started looking into a singleton model. So that the player can store the important info (character being controlled, target) so that the ui objects can just talk to the player instead of having to get and set data from the character map object itself. Which I first tried to use statics for but I really don't know there ins and outs so that is hard. As well as singletons. My second question is If I do implement a singleton model for the player object, do I use static var's? Or should I design the player to work with the interface and not try to encapsulate the player. Sorry if my noobery is hard on ur eyes.

    I would post code but I am work so I can't atm.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,184
    First of all, GetComponent isn't that bad. You can safely do a couple of those every frame without it causing any problems.

    That being said, the usual way to fix having to ask for things all the time is to simply store a reference to them. Instead of making player a singleton, and going Player.instance.transform.position every frame, you just store a reference to the player's transform once, and then check it's position every time you need to know it's position.

    Post some example scripts when you get home, that'll get you some specific help. Making the player a singleton isn't a bad idea if you're only going to have one player ever.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sort of. Singletons also lead to major code dependencies. Not a problem in small projects, but they can cause all sorts of trouble in big projects.
     
    TonyLi likes this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Thank you! Pardon the off-topic rant, but people too often set black-and-white rules. ("GetComponent is bad! SendMessage is bad! Carbs are bad! Fats are bad!") I've seen some horrendous code going through gymnastics to avoid a single SendMessage that's only called once during the entire game. I hope people get into the habit of profiling more, or at least evaluating performance within context. [/rant...]
     
    KelsoMRK and Kiwasi like this.
  5. ninja_gear

    ninja_gear

    Joined:
    Jul 31, 2013
    Posts:
    15
    I dropped the statics and singleton idea for plain old fashioned OOP: private variables, public getters and setters, with as many pointers for the transforms, motors, and components that I need. It's a lot more typing but way less thinking and I'm sure a higher degree of readability.
     
  6. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Very true, but events are just plain easier to use and better than send message. But ya in general I see to many people worrying about non issues since they don't bother to profile.

    Also non of is bad you just see people do bad things, such as doing a GetComponent in the update loop