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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Accessing the same named function in different scripts

Discussion in 'Editor & General Support' started by iso250, Jan 17, 2016.

  1. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    Hello all.

    In my game I have a script on each level that generates a location for my enemies to pathfind to. The function in every script has the same name (as shown below) but the code in each script is different as the levels are different. The name of the function in each script is...

    public void RandomLocationGenerator ( float EnemyType, out float XDestination, out float YDestination, out float ZDestination )

    The function accepts what type of enemy is calling the function as certain enemies like to hang around certain areas and the function feeds back a set of coordinates for the enemy to move to.

    What I can't figure out is how to get the enemy to call upon the script on the level that it's on. I have looked at some tutorials and seen Interfaces but I'm not sure if that will apply as the RandomLocationGenerator on every level has different code.

    If Interfaces is what I'm looking for, then I must admit that I'm not sure how to apply it. The tutorial example provided here...

    https://goo.gl/GYJB8P

    doesn't quite answer all of my questions. Would the level script or the enemy script be the interface? If the enemy script is the interface section then do I move the rest of the enemy code from the public class section in to the interface section or can I call the interface code from within the public class section?

    If interfaces is not the solution I require, can someone please point me in the right direction?

    I'm hoping someone can help me as this represents one of the last things I need to complete my game!!

    Thank you.
     
  2. iso250

    iso250

    Joined:
    Apr 19, 2015
    Posts:
    28
    So there's no way to do it? That's a shame. i thought Unity would be able to manage that somehow.
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Of course you can do it. You pretty much answered your own question, interfaces. You also use a base class, but probably interface makes the most sense.