Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED] How can I exit a child method from the parent method?

Discussion in 'Scripting' started by Deleted User, Nov 5, 2014.

  1. Deleted User

    Deleted User

    Guest

    Let's say I have

    public virtual void Activate(){
    // Do stuff here
    if (true){
    return child.Activate();
    }
    }

    public override void Activate(){
    base.Activate();
    }
     
  2. Deleted User

    Deleted User

    Guest

  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    what?

    You're going to have to rephrase your question. I have no idea what you're asking for.

    Also, use code tags!
     
  4. Deleted User

    Deleted User

    Guest

    OK you know how you can use "return;" to exit the method and stop it from executing the rest of the code?
    Well I want to stop the exution of the method from the parent method. Is this possible? I know I could easily do it by using a boolean in the child method but I don't want to.
     
  5. Deleted User

    Deleted User

    Guest

    Not possible?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    So you want to be able to say something like 'super return' in the parent class so that all operation of the call to the method stops?

    Code (csharp):
    1.  
    2. public class BaseClass
    3. {
    4.     public virtual void Foo(bool condition)
    5.     {
    6.         Debug.Log("Doing some Foo work.");
    7.    
    8.         if(condition)
    9.         {
    10.             Debug.Log("Cancelling all foo work!");
    11.             super return;
    12.         }
    13.        
    14.         Debug.Log("Finishing Foo work in base.");
    15.     }
    16. }
    17.  
    18. public class ChildClass : BaseClass
    19. {
    20.  
    21.     public override int Foo(bool condition)
    22.     {
    23.         Debug.Log("Overriding some Foo work BEFORE calling base.");
    24.        
    25.         base.Foo(condition);
    26.        
    27.         //this only will occur if 'condition' was false
    28.         Debug.Log("Overriding some Foo work AFTER calling base.");
    29.     }
    30.  
    31. }
    32.  
    Yeah, does NOT exist.

    You'd have to implement this your own way... something like a referenced bool or something.
     
    Deleted User likes this.
  7. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Edit - rereading this and I think I was misunderstanding the question. Yea Im not sure what you are asking for exists.
     
    Last edited: Nov 7, 2014
  8. Deleted User

    Deleted User

    Guest

    Thanks. Exactly what I was talking about.