Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AlphaGarg8447 · Oct 11, 2015 at 05:50 PM · c#errornetworkingphotongetcomponent

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image imp903 · Oct 12, 2015 at 12:28 AM 0
Share

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.

avatar image iwaldrop · Nov 25, 2015 at 04:51 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

41 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] PhotonNetwork.Instantiate cannot spawn players 0 Answers

C# Photon Networking - Preventing duplicate GameObjects from spawning on Join. 1 Answer

Network Rigidbodies act weird? 0 Answers

Check what group id you have after being instantiated. (PUN) 1 Answer

Using GetComponent without knowing the script name? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges