Search Unity

Major change worth noting in UNITY5!!! (use of componenets)

Discussion in 'Scripting' started by CastleIsGreat, Mar 3, 2015.

  1. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Hi all, with unity 5 finally released, I've found it important to point out to the new users of unity 5, that you must now declare components meaning, use .GetComponent<Type>();

    Those who have been using the beta know what I mean, E.G.

    Say I want to use something in the networkView component of GameObject Hero.

    The script would in turn have to have two things,
    Using UnityEngine;
    Using System.Collections;

    Public Class HeroNetworking : MonoBehaviour {

    GameObject Hero;
    NetworkView networkView;

    void Start(){
    networkView = Hero.GetComponent<NetworkView>(); // NetworkView of Hero.
    }
    }


    Now that being said I've only used Unity 5, so I may be wrong, but I believe this is different than how components were handled previously inside of unity previous to Unity 5. If I'm wrong than my bad. If not now you know why your components don't seem to be working!

    OR as Eric clarifies the real changes below and teaches teaches me below an easier way of doing it in the process...

    Hero.GetComponent<Type>().function = blah....

    Or since Hero is the parented GameObject simply...

    GetComponent<Type>().function = blah....
     
    Last edited: Mar 4, 2015
  2. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi Castle,

    Yes, this was documented in a blog post... I forget how many months ago. I guess if someone hasn't read the blog posts then it would come as a surprise.
     
  3. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Indeed, I thought i'd throw up a post about it, as I'm sure there will be questions about it in the near future popping up on the forums lol.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This isn't correct, since GameObject isn't a component, and the hero variable was already declared as a global GameObject variable above. The code should just be:

    Code (csharp):
    1. networkView = hero.GetComponent<NetworkView>();
    The difference in Unity 5 is that the component shortcut properties have been removed. So whereas these are equivalent in Unity 4:

    Code (csharp):
    1. rigidbody.velocity = Vector3.zero;
    2. GetComponent<Rigidbody>().velocity = Vector3.zero;
    In Unity 5 the only option is GetComponent. The exception is Transform...the shortcut for that still exists since you can't have a GameObject without a Transform component. So this is still fine:

    Code (csharp):
    1. transform.Translate (Vector3.up);
    --Eric
     
    hippocoder likes this.
  5. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    You're right. Its late. lol fixing it now. Additionally I wasn't aware you could use "GetComponent<type>.variable =" Useful to know. I'm still pretty new to programming myself, I'd like to pretend I'm learning it well though!
     
    knr_ likes this.
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I should also note that components are a flat hierarchy and therefore GetComponent is not chained. This was apparently a point of confusion for at least one person in a previous discussion, where the complaint that something like

    Code (csharp):
    1. camera.rigidbody.audio.volume = 0;
    would become

    Code (csharp):
    1. GetComponent<Camera>().GetComponent<Rigidbody>().GetComponent<AudioSource>().volume = 0;
    Which is technically true except it's not something you would ever do! The correct code is just

    Code (csharp):
    1. GetComponent<AudioSource>().volume = 0;
    So if you ever have more than one GetComponent in a row, you are Doin' It Rong™.

    --Eric
     
    kdubnz likes this.
  7. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Also a great point!

    Now ofcourse all of these are assuming its the parent object of the script you're trying to pull from.

    but with another public GameObject on the other hand things get a little more complicated, which is why I choose to write them out as variables.
     
    Last edited: Mar 4, 2015
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nope, there are no exceptions. Components aren't nested so there's never a reason to chain GetComponent calls.

    --Eric
     
  9. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    I suppose what I'm saying is you cant say

    Public GameObject Cow;

    Cow.GetComponent<Transform>.position = Vector3(0,0,0);

    You'd have to write

    Transform trans = Cow.GetComponent<Transform>();

    trans.position = Vector3 (0,0,0);

    But you are completely corrrect that you could call ANY component on any tier since the heirarchy is flat.
     
    Last edited: Mar 4, 2015
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sure you can, but you don't need to, since, as mentioned above, Transform is the exception to the "no more shortcuts" rule.

    cow.transform.position = Vector3.zero;

    --Eric
     
  11. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    Odd... I just tried it and I was getting errors... Let me try again...

    OK ON A SIDE NOTE, When I go to copy and paste out of MonoDevelop It turned into chinese. This is really funny and strange.

    ℼ佄呃偙⁅呈䱍倠䉕䥌⁃ⴢ⼯㍗⽃䐯䑔䠠䵔⁌⸴‰牔湡楳楴湯污⼯久㸢䠼䵔㹌䈼䑏㹙䘼乏⁔慦散㴠✠潍潮㸧匼䅐⁎瑳汹⁥‽挧汯牯⌺〰㘹㔹✻㸠異汢捩⼼偓乁㰾偓乁猠祴敬㴠✠潣潬㩲㐣㐴㐴㬴‧☾扮灳㰻匯䅐㹎匼䅐⁎瑳汹⁥‽挧汯牯⌺㌳㌶㑡✻㸠慇敭扏敪瑣⼼偓乁㰾偓乁猠祴敬㴠✠潣潬㩲㐣㐴㐴㬴‧☾扮灳㰻匯䅐㹎匼䅐⁎瑳汹⁥‽挧汯牯⌺㐴㐴㐴✻㸠潃㱷匯䅐㹎匼䅐⁎瑳汹⁥‽挧汯牯⌺㐴㐴㐴✻㸠㰻匯䅐㹎⼼但呎㰾䈯䑏㹙⼼呈䱍>娀.. WHAT? LOL

    I suspect for some reason its turning into Alt codes... I have no idea why... LOL I suspect it has to do with forum though, because its fine going to a text doc.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Move : MonoBehaviour {
    5.  
    6.    public GameObject Cow;
    7.  
    8.    // Use this for initialization
    9.    void Start () {
    10.      Cow.GetComponent<Camera>.enabled = false;
    11.    }
    12.  
    13.    // Update is called once per frame
    14.    void Update () {
    15.  
    16.    }
    17. }
    18.  
    ERROR:

    Assets/_Scripts/GameControllers/Move.cs(10,21): error CS0119: Expression denotes a `method group', where a `variable', `value' or `type' was expected
     
    Last edited: Mar 4, 2015
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's GetComponent<Type>(), not GetComponent<Type>.

    --Eric
     
  13. CastleIsGreat

    CastleIsGreat

    Joined:
    Nov 10, 2014
    Posts:
    176
    That's it you are certifiably my new favorite person on the forums.

    The amount of junk code you've just gotten rid of is almost funny if it wasnt so sad.

    BUT NOW I'm determined to find a use for making a variable...

    Perhaps to store the data, E.G. as an array?

    Transform[] PlayersTrans = GetComponentsInChildren<Transform>();
     
    Last edited: Mar 4, 2015
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I always like getting rid of junk code!

    --Eric
     
  15. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    [offTopic]
    >>> you are Doin' It Rong™

    :D

    [/offTopic]
     
    CastleIsGreat likes this.
  16. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, caching a GetComponent call using a variable is faster. But it's only worth doing if you access that component a lot (I mean really a lot).

    --Eric
     
  17. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    I believe the reason for removing the shortcuts is that GetComponent has built-in caching now, removing the need for the caching features those shortcut properties previously satisfied.
     
  18. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  19. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    Seems logical actually, since everything is a GameObject, and everything has a transform whether it's visible or not.

    Not sure how that happened, but MD isn't known for stability, consistent behaviour or usability as an IDE. Paste the results into Google Translate for some wacky cut-up poetry though :)