Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 robvdbogaert · Nov 30, 2015 at 12:48 PM · functionsactivatejs-c#-together

How to remotely (with a different C# script or js) activate function from the main C# script?

I've been searching and really havent found a solution that I could understand.

I want it as simple as possible, this is what I have:

C# function from my main script file called "mainScript.cs":

 public void OpenBuyMenu ()
     {
         BuyMenu.enabled =! BuyMenu.enabled;
         GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled =! GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled;
     }

I want this function to execute when the player is within a trigger area of a certain object and the key M is pressed. Now I'm very new to C#, but in js I can build the script all the way up to activating the function above. This is the js file that could do all that:

 private var enter : boolean; //this is enabled when the player is ontriggerentenr from the door
 var ShowGUILabel : boolean = true; //Put it on if you want to read "press f to open the door" when you are near the door
 
  function Update (){
           if (Input.GetKeyDown(KeyCode.M) && enter == true)
          {
             //what goes here?
          }
  }
  
  function OnGUI () {
  
      if(ShowGUILabel == true && enter == true)
      {GUI.Label(Rect(Screen.width / 2 - 100, Screen.height -100, 250, 100), "Press [M] to open Menu. ");}}
 
  //Activate the Main function when player is near the door
  function OnTriggerEnter (other : Collider){
  if (other.gameObject.tag == "Player") {
  enter = true;
  }
  }
  
  //Deactivate the Main function when player is go away from door
  function OnTriggerExit (other : Collider){
  if (other.gameObject.tag == "Player") {
  enter = false;
  }
  }

Now what I would like to know is, can I do this the easy way by adding some js coding on the spot where it says '//what goes here?" or maybe even better, how can I activate the function when the player is in the trigger area AND de M key is pressed by just using C#?

Thanks for the help!

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

1 Reply

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

Answer by Mich_9 · Nov 30, 2015 at 02:13 PM

You have to reference the mainScript.cs class in your other script, this is the code translate to C# of the Trigger Objetc.

 using UnityEngine;
 
 public class Trigger: MonoBehaviour
 {
 
 private bool enter; //this is enabled when the player is ontriggerentenr from the door
 private bool ShowGUILabel = true; //Put it on if you want to read "press f to open the door" when you are near the door
     public mainScriptClassName BuyMenu;//The reference of your mainScript.cs class
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.M) && enter == true)
         {
             BuyMenu.OpenBuyMenu();//Call the function OpenBuyMenu in mainScript.cs
         }
     }
 
     void OnGUI()
     {
 
         if (ShowGUILabel == true && enter == true)
         {
             GUI.Label(new Rect(Screen.width / 2 - 100, Screen.height - 100, 250, 100), "Press [M] to open Menu. ");
         }
     }
 
     //Activate the Main function when player is near the door
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             enter = true;
         }
     }
 
     //Deactivate the Main function when player is go away from door
     void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             enter = false;
         }
     }
 }
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

34 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

Related Questions

Active object when all the tags are in the collider 1 Answer

Can't activate unity ads: "Forbidden" 1 Answer

Any reason why global vars are null within certain functions? 0 Answers

Method Group 1 Answer

function being called twice at the same time causing an error. 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