Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TheCodingTraceur · May 27, 2014 at 10:16 PM · c#functionclass

How do I use a function in a class more than once?

I am writing a script that will allow users to implement a GUI of the player statistics. One of the features is collision based showing and hiding. The following code throws Error CS0111: Type 'PlayerStatisticsGUI' already defines a member called 'OnTriggerEnter' with the same parameter types (CS0111) Here is the code:

 //Show player stats when trigger is collided with
     private void OnTriggerEnter(Collider ShowTrigger) {
         Show = true;
     }
 
     //Hide player stats when trigger is collided with
     private void OnTriggerEnter(Collider HideTrigger) {
         Show = false;
     }

What is the solution to this? Is there a workaround?

Comment
Add comment
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

4 Replies

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

Answer by christoph_r · May 27, 2014 at 10:29 PM

I don't think your code is doing what you think it does. By naming it ShowTrigger and HideTrigger you just assigned by which name you are going to call the collider that collided with your trigger. You need to tell your script what to do once a collider enters a trigger depending on certain conditions if you want different outcomes. In your case, you could simply write:

 private void OnTriggerEnter(Collider col)
     {
         if(Show=true)
         { Show = false;}
         else
         { Show = true;}
     }

Or more elegantly:

 private void OnTriggerEnter(Collider col)
     {
         Show = !Show;
     }

This toggles the variable show whenever something enters the collider. By the way, variables usually begin with lower case characters so they're not confused with methods, for example.

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
avatar image
0

Answer by seandanger · May 27, 2014 at 10:21 PM

You can't have 2 separate functions named the same in the same class, otherwise the compiler won't know which one is which.

The simplest thing you may need is to create 2 separate scripts, one for your HideTrigger, and one for your ShowTrigger. Otherwise, you could use some conditional code in your OnTriggerEnter function, like so:

 public class TriggerExample : MonoBehaviour
 {
     public bool showOnTriggerEnter = false; // change this via the inspector as necessary
 
     private void OnTriggerEnter(Collider trigger)
     {
         Show = showOnTriggerEnter;
     }
 }

Then you'd attach that component to each trigger, and set the showOnTriggerEnter variable to true for the one, and false for the other.

Comment
Add comment · Show 1 · 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
avatar image TheCodingTraceur · May 27, 2014 at 10:23 PM 0
Share

What do you mean create 2 different scripts? How would I make that work?

avatar image
0

Answer by meat5000 · May 27, 2014 at 10:21 PM

Don't repeat the function.

Use (Collider trigger) and probe trigger to find the info you need. The name of the Collider inside those Argument brackets will have no bearing or connection to any actual world objects. It's just a receiver for information.

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
avatar image
0

Answer by Kiwasi · May 27, 2014 at 10:32 PM

Here is the solution

 private void OnTriggerEnter(Collider other){
     if (other.name = "ShowTrigger"){
         Show = true;
     } else if (other.name = "HideTrigger"){
         Show = false;
     }
 }

The why is slightly more complicated. Not sure how well I can explain it. You might want to refer to some c# tutorials

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I access a function without knowing the script/class name to which it belongs? 1 Answer

Make inspector show changes of class variable value in function? 1 Answer

Unity C# Start function without extending Mono 2 Answers


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