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 Faniha · May 13, 2015 at 05:10 PM · c#2dfunctionexecute

check if part of function is executed in another script

rather new to programming.

I'm making a 2d game where you control a hand and have to defend against insects. I would like the insects to be destroyed when the controls (gameobjectet) are on top of the insects and a given function from the script/class associated with the controls are called.

I need to check if the function in question (Called: Attack) is called from another script that is associated with the insects. I thought to do like demonstrated in this thread: http://answers.unity3d.com/questions/653021/how-to-check-if-a-function-is-called.html

but the function 'Attack' has a few 'if statements' within and I only want the insects to be destroyed if one specific if statement is carried through.

How do I check if part of a function in another script is executed?

Comment
Add comment · Show 4
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 fafase · May 13, 2015 at 06:24 PM 0
Share

Since you are new to program$$anonymous$$g I guess event is too far fetched but that would be the way I would use.

Having an event called at that specific part and the other class listening to it.

$$anonymous$$ore simple way would be to have the Attack method to call a method on the other script which turns out to become spaghetti code as everyone interferes with everyone.

avatar image Faniha · May 13, 2015 at 08:00 PM 0
Share

ok.. hmm would this be a good tutorial to follow? https://www.youtube.com/watch?v=N2zdw$$anonymous$$IsXJs

And if yes, how would I use the different parts he mention (listener, maneger etc.) to get the effect I want? biggest question how do i make so the event only happens in a certain part for the attack method?

I added a simplify version of the attack method - i only want an event to happen when "Input SucessFG" happens

 float timeframe = 0;
 bool chkinput=false;
 
 public void Attack (){
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.F)) {    
         chkinput = true;
     }
     if (chkinput) {
         timeframe += 1 * Time.deltaTime; 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G)) {
             print ("Input SucessFG");
             timeframe = 0;
             chkinput = false;
         }
         if (timeframe > 1) {
             print ("Time Out");
             timeframe = 0;
             chkinput = false;
         }
     }
 }
avatar image fafase · May 13, 2015 at 08:36 PM 0
Share

you create an event and you subscribe a listener. These are the main ideas.

 public event Action OnSomethingDone = ()=>{};
 
 public void Attack(){
    if(condition){
       OnSomethingDone();
    }
 }

This is for the event. It uses the generic way ins$$anonymous$$d of the old school delegate way shown on the video but the result is the same.

Now for the listener:

 void Start(){
     OtherType other = GetComponent<OtherType>();
     other.OnSomethingDone += Listener$$anonymous$$ethod;
 }
 void OnDestroy(){
     OtherType other = GetComponent<OtherType>();
     other.OnSomethingDone -= Listener$$anonymous$$ethod;
 }
 void Listener$$anonymous$$ethod(){}

Note when there are parenthesis and when there is none. Pretty much it.

avatar image Faniha · May 14, 2015 at 07:58 AM 0
Share

honestly I still don't really get it.. I tried and failed.

1 Reply

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

Answer by fafase · May 14, 2015 at 08:00 AM

 public class EventClass:MonoBehaviour{

     public event Action OnClick = ()=>{};
     public void Attack (){
         if (Input.GetKeyDown (KeyCode.F)) {    
            chkinput = true;
         }
         if (chkinput) {
            timeframe += 1 * Time.deltaTime; 
            if (Input.GetKeyDown (KeyCode.G)) {
              print ("Input SucessFG");
              OnClick();
              timeframe = 0;
              chkinput = false;
            }
            if (timeframe > 1) {
              print ("Time Out");
              timeframe = 0;
              chkinput = false;
            }
        }
    }
 }

 public class BugMove:MonoBehaviour{
     private EventClass eventClass = null;
     void Start(){
          eventClass = GetComponent<EventClass>(); // If on another object, use GameObject.Find in front
          if(eventClass != null){
              eventClass.OnClick = this.OnClickDetected;
          }
     }
     void OnDestroy(){
          if(eventClass != null){
               eventClass.OnClick -= this.OnClickDetected;
               eventClass = null;
          }
     }
     private void OnClickDetected(){ Debug.Log("Gotcha!");}
 
 }

So the first class has the checks and calls OnClick(). At that point, the EventClass does not care if it has any listener, it just calls the method, it could be there is no one listening.

BugMove is the listener, and it is done in the Start. It finds the component and register to the event with :

 eventClass.OnClick = this.OnClickDetected;

Now, any time OnClick is called, it will also called OnClickDetected. OnDestroy removes the listener but it can be done anywhere.

Think of it like a following on twitter. EventClass is the famous person and you are the BugMove. When you press Follow, you do what is in the Start. From now on, any time the guy tweets, you get a feed of it.

Comment
Add comment · Show 3 · 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 Faniha · May 14, 2015 at 08:17 AM 0
Share

ok this makes sense to me thanks :)

just two quick questions

  • can EventClass be placed on an object?

  • in regard to Action in line 3.. what does it refer to? right now it gives me an error

avatar image fafase · May 14, 2015 at 06:14 PM 1
Share

EventClass inherits $$anonymous$$onoBehaviour and is just a basic script. The name may confuse you to think it is some kind of framework class but it might as well be called AdamSandler, but then it loses the fun.

Action, on the other hand, is a framework keyword. Exactly, it is a generic class representing a delegate that returns void and in this case takes no argument. Plenty about it on the net so look it up.

The event keyword turns our delegate into an event. All in all, it pretty much does the same with some restrictions that can be usefull.

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/delegates

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/events

It is all there.

avatar image Arkin160 · Apr 21, 2016 at 07:06 PM 0
Share

thanks a lot it's help me :)

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

20 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

Related Questions

2D noise function to generate voxel circles 1 Answer

C# how to Access UI Assigned to Function 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

my enemy is broken 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