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

Question Same method that modifies local variables on two script?

Discussion in 'Scripting' started by NotMyUsernameAgain, Aug 5, 2023.

  1. NotMyUsernameAgain

    NotMyUsernameAgain

    Joined:
    Sep 28, 2017
    Posts:
    126
    Hi everyone,

    so I created just a second script, which basically does almost the same thing as method from another script.
    Basically checking for local variables and modifies it.

    I don't want to copy the whole code, since I'd need to change both.

    EDIT:
    OK, I use inheritence.
    Sorry, forgot that the inherited variables are their own and not the same of the parent class.
    Rarely using inheritence (despite "MonoBehaviour")
     
    Last edited: Aug 6, 2023
  2. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Make an interface, a common class or some kind of component?
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I'm thinking you're looking for Inheritance, but I'm not fully sure what you mean. As the parent script can hold the variable declarations and public functions, that each child can call or use differently.

    Unless you mean you just want to call a function/method and have it modify 2 different scripts?
     
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    You can also declare a delegate which is a reference to a function, assign that function then pass the instance of the delegate as an argument. It really depends on the context.
     
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    524
    "First make it work then make it right"... if you have two classes that work post them so we can see them and perhaps offer a concrete alternative to what you have. Use the code tags.