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?
I believe the first way you did it is correct (granted, I don't quite understand your photon network call, but I'm going to assume you reference the gameobject correctly). I think the issue might only be syntactically from what I can tell at first glance. If you use parentheses, to my understanding, you have to declare the argument inside them as a string surrounded by quotes. If you do it by type like you are though, I believe you use GetComponent(); ins$$anonymous$$d. So really I believe you can just change the line "componentenabler compEnblr..." to
componentEnabler compEnblr = myPlayerGO.GetComponent<componentEnabler>();
and it should be fine as far as I can tell. Outside of that maybe check things like capitalization to make sure that's all in order, but I believe you are using the correct way to reference it.
What you're doing is storing a reference to an instance of an object. The Enable method does not return an object of type 'componentEnabler', so you're going to experience a compile error when trying to do that.
So, as imp903 suggests, the first snippet is probably what you want. But you could also get away with the second snippet of you simply refactor it such that it is no longer an assignment to a variable.
Answer by iwaldrop · Nov 25, 2015 at 04:54 PM
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();
Or
var e2 = GetComponent(typeof(componentEnabler));
Will both do the same thing. Give it a shot!