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

How do I run a function at runtime that isn't one of the functions already built in to Unity?

Discussion in 'Scripting' started by Badoobicus, Feb 9, 2015.

  1. Badoobicus

    Badoobicus

    Joined:
    Jan 27, 2013
    Posts:
    10
    I am working on an RTS game where all of the units and buildings are subclasses of one class. I want a Start() function to set up some stuff for these units (i.e. set their color to the proper team's color). However, for specific units, if I want to add some stuff to the Start() function, I want to be able to run it without having to copy the code from the Start() function in the main class and then add my stuff on to the end. If I do this, then when I change something in the main class, I will have to go through and change it in all of the other classes. I know how to declare functions, but how can I make my own function that runs at runtime similar to Start() but isn't a function already built into Unity?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    so the base has a Start(), but for some of the subs you want their Start to do addidional things?
    Can't you just put base.Start() in the overridden methods, then add the extra code?
     
    Jessy likes this.
  3. Badoobicus

    Badoobicus

    Joined:
    Jan 27, 2013
    Posts:
    10
    The problem is that if I change something in the base class, I would have to go through and paste the code to every subclass that overrides the base class.
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It sounds like you don't understand what the base keyword does. Do you?
     
  5. meat5000

    meat5000

    Joined:
    Mar 5, 2013
    Posts:
    118
    I suggested Inheritance. Did I misunderstand like picklerok said?

    We don't allow cross-posting on UA (lately). As your QA lives here now I'll just go and send the other back to oblivion.
     
    Jessy likes this.
  6. Badoobicus

    Badoobicus

    Joined:
    Jan 27, 2013
    Posts:
    10
    Yeah, I don't think I'm using it right. That may be the point of confusion. I am saying that in the class that all of the other classes are inheriting I want to create my own method (not Start() or Awake()) that will run once when any script that inherits the main one is started. Sorry for any misuse of terminology.
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    may not compile:
    Code (CSharp):
    1. class myBase {
    2.   void method () {
    3.     //stufff...
    4.   }
    5. }
    6.  
    7. class mySub : myBase {
    8.   void something () {
    9.     base.method();
    10.     //other stufff....
    11.   }
    12. }
    13.  
    14. //or even
    15. class mySub : myBase {
    16.   override void method() {
    17.     base.method();
    18.     //more method stufff....
    19.   }
    20. }
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Use composition instead of inheritance. It avoids all the subclass issues you described. (BTW, never ever copy the same lines of code into another script. If you find yourself about to do this, figure out how to restructure your scripts so they share the code.)

    For an example of composition, if your GameObject is a production building, add a ProduceUnits component. If it's a tower, add a Defend component. If it's a fighter, add a FightWhenAttacked component. If the fighter is a "hero" unit, you might also add a SnarkyOneLiners component. Each of these components can do their thing in their own Start() methods. They can use their Awake() methods to find each other if they need to communicate in Start().