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

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();

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 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....

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.

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.

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:

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:

rigidbody.velocity = Vector3.zero;
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:

transform.Translate (Vector3.up);

–Eric

1 Like

You’re right. Its late. lol fixing it now. Additionally I wasn’t aware you could use “GetComponent.variable =” Useful to know. I’m still pretty new to programming myself, I’d like to pretend I’m learning it well though!

1 Like

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

camera.rigidbody.audio.volume = 0;

would become

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

GetComponent<AudioSource>().volume = 0;

So if you ever have more than one GetComponent in a row, you are Doin’ It Rong™.

–Eric

1 Like

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.

Nope, there are no exceptions. Components aren’t nested so there’s never a reason to chain GetComponent calls.

–Eric

I suppose what I’m saying is you cant say

Public GameObject Cow;

Cow.GetComponent.position = Vector3(0,0,0);

You’d have to write

Transform trans = Cow.GetComponent();

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.

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

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.

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour {

   public GameObject Cow;

   // Use this for initialization
   void Start () {
     Cow.GetComponent<Camera>.enabled = false;
   }
  
   // Update is called once per frame
   void Update () {
  
   }
}

ERROR:

Assets/_Scripts/GameControllers/Move.cs(10,21): error CS0119: Expression denotes a method group', where a variable’, value' or type’ was expected

It’s GetComponent(), not GetComponent.

–Eric

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();

I always like getting rid of junk code!

–Eric

[offTopic]
>>> you are Doin’ It Rong™
:smile:
[/offTopic]

1 Like

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

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.

There’s no built-in caching. The reason for removing the shortcuts is to help make Unity more modular in the future. http://blogs.unity3d.com/2014/06/23/unity5-api-changes-automatic-script-updating/ (Note that the claim on that page that the Transform shortcut is cached is incorrect. It’s faster than GetComponent, but still slower than manually caching it.)

–Eric

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 :slight_smile: