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;
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...
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:
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:
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!
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
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
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.)
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