Referencing a function in a C# script

To start off, let me explain what I’m trying to achieve. There’s this asset on the Asset Store called Photon Network, and it works as an easier way of making a game multiplayer. So just ignore all the special stuff it adds to the scripting and consider the Instantiate below a regular one. There is also a C# script I created called ComponentEnabler with a void that tells mainCam, motor and manager to get themselves enabled, and that script works fine the way it is.

But, since nothing in scripting is as easy as it should be, my NetworkManager script is getting errors at line 63, where the compEnblr variable is created as the ComponentEnabler. But, instead, it says “Expression denotes a type, where a variable, value or method group was expected”. I searched around on Google but got bored to the point of feeling empty.

		GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

		componentEnabler compEnblr = myPlayerGO.GetComponent(componentEnabler);
		compEnblr.Enable ();

If only I knew what was happening, then maybe I would be able to solve it. Also, if I try to not make a temporary variable and instead do this:

		componentEnabler compEnblr = myPlayerGO.GetComponent(componentEnabler).Enable ();

Then it doesn’t recognize the function as being from componentEnabler. It just says it doesn’t exist. Any ideas?

The reason it’s failing is because of how you’re using the GetComponent method. There are two ways to do it, using a generic parameter and by passing a type as a method parameter.

var e1 = GetComponent< componentEnabler >();

Or

var e2 = GetComponent(typeof(componentEnabler));

Will both do the same thing. Give it a shot!