0

I duplicated level 1 of my game. I want to make it work the same way as Level 1 with a minor addition to the previous one. A script and a collider are attached to the prefab where a collider function is used in two classes of the same script. I am using two classes in a script. In level 2 of my game, I want to call a second class and want the script to execute collider function of second class. How can I call Second class in Level 2? please help me. Thank you.

public class class1: Monobehaviour {
//public variables declaration

void OnTriggerEnter2d(){}

}

public class class2: Monobehaviour {
//public variables declaration

void OnTriggerEnter2d(){}

}
  • OK, I don't quite understand what you're talking about but let me guess. You want to call a method that's in `SecondClass` from `Level2`, right? Then you just call `SecondClass.YourMethodName()` in `Level2`. If it's not static then create an instance of `Class2` first and then call the method. Also, you don't need to duplicate `Level1` to get `Level2`. There is something called inheritance. If this is your problem, please tell me to post it as an answer to further explain it. – Sweeper Aug 04 '15 at 08:03

1 Answers1

0

You could create an interface and have both classes implement the same interface.

public interface ITriggerBehaviour
{
    void OnTriggerEnter2d();
}

public class class1: Monobehaviour, ITriggerBehaviour
{
    //public variables declaration

    void OnTriggerEnter2d(){}
}

public class class2: Monobehaviour, ITriggerBehaviour
{
    //public variables declaration
    void OnTriggerEnter2d(){}
}

Then, without knowing which class implements the method, you call it by the same name.

public void SomeOtherFunction()
{
    // In your code, the object will be provided elsewhere, in which case
    // you may want to use the 'as' operator to convert the object
    // reference to the interface and test for 'null' before using it.
    // This example shows that an interface can be used to hold a reference
    // to different types of object, providing they both implement the
    // same interface.
    ITriggerBehaviour someObject;
    if(currentLevel == 1)
        someObject = new class1();
    else
        someObject = new class2();

    // Call the method via the interface
    someObject.OnTriggerEnter2d();
}
Evil Dog Pie
  • 2,300
  • 2
  • 23
  • 46
  • _[Prefix interface names with the letter I, to indicate that the type is an interface](https://msdn.microsoft.com/en-us/library/8bc1fexb(v=vs.71).aspx)_. Also the cast in `new class1() as OnTriggerBehaviour` is not necessary –  Aug 04 '15 at 08:08
  • @MickyDuncan Yes, I could do, that's a common practice, although it is only a convention. I should also change the name from `OnTriggerBehaviour`, sine the 'On' part is commonly used to name a method that raises and event. – Evil Dog Pie Aug 04 '15 at 08:15
  • @MickyDuncan Again, that's true in this instance, particularly as I don't check for `null` after the cast. If the object were being supplied from elsewhere it may not be known that it implements the interface, hence the use of `as`. redundant in this iexample, as you say. – Evil Dog Pie Aug 04 '15 at 08:17
  • Agreed, the compiler doesn't really mind. The problem though is that I don't know if `OnTriggerBehaviour` is an interface or a **method**. The `On` prefix is just the more confusing for readers –  Aug 04 '15 at 08:18
  • OnTriggerEnter2D is a function/method. – Anonymous Coder Aug 04 '15 at 08:21
  • Level1 -> OnTriggerEnter2D – Anonymous Coder Aug 04 '15 at 08:25
  • Level2-> OnTriggerEnter2D with additional gameObjects – Anonymous Coder Aug 04 '15 at 08:26
  • Can I add changes with in a class1 or class2 alongside interface? – Anonymous Coder Aug 04 '15 at 08:26
  • Thanks a bunch by the way. Since I'm beginner so I'm having understanding classes things. OOP – Anonymous Coder Aug 04 '15 at 08:27
  • @AnonymousCoder You can implement completely different code in the two classes, as long as the function prototype conforms to the contract of the interface. If you use the interface to make the call the code implemented in the corresponding class behind the interface will be run. The interface is used to hide the implementation behind an [abstraction](http://stackoverflow.com/q/8960918/3581917) (link is a Java question, but the OO principals are the same). – Evil Dog Pie Aug 04 '15 at 08:33
  • And what if I want to use a whole class(all methods in a class) instead of single method? – Anonymous Coder Aug 04 '15 at 08:41
  • I mean calling the whole class instead of a method of a particular class? – Anonymous Coder Aug 04 '15 at 08:42
  • You don't call a class. You can include as many methods and properties in the interface as you require. It is actually good practice to do so, as it allows you to create 'mock' versions of the interface that you can use to test other parts of the code that use it. But unit testing is beyond the scope of this question. In the context of a game, it may also allow you (in the future) to supply new content by adding classes in add-on packages - or even allow other people to develop their own levels for your game. – Evil Dog Pie Aug 04 '15 at 08:50