Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jonjuu · Feb 25, 2020 at 09:17 PM · script referencing

Reference to a general type of script independent of its class name

I want to have a class to reference a script, but that script is not the same every time. For example:

public class PlayerController : MonoBehaviour {

 public InputActions inputActions;

 public MonoBehaviour jumpController;

 private void Awake()
 {
     inputActions.PlayerActions.Jump.performed += context => jumpController.Jump();
 }   

}

I have 3 classes for JumpController (with differents class names) that implement the same methods (same names) but in differents ways. I want to switch in the inspector between these classes without change the code in the PlayerController class. The problem is that I can't access to a script methods without specificing his exact name. Is there a way to accomplish this?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tormentoarmagedoom · Feb 25, 2020 at 09:19 PM 0
Share

Hello.

You can just declare it as a Object.

 Object FinalScript;
 ScriptA TheScriptA;
 ScriptB TheScriptB;
 
 
 FinalScript = TheScriptA;

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Hellium · Feb 25, 2020 at 09:42 PM

You are looking for Interfaces.

  public interface IJumpController
  {
      void Jump();
  }

 public class PlayerController : MonoBehaviour {
      public InputActions inputActions;
      public GameObject jumpController; // Unity can't serialize interfaces
      private void Awake()
      {
          inputActions.PlayerActions.Jump.performed += context => jumpController.GetComponent<IJumpController>().Jump();
      }  
 }

Then, make your classes with the Jump method implement the IJumpController interface

 public class ExampleJumpController : MonoBehaviour, IJumpController

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fragsteel · Feb 25, 2020 at 11:11 PM 2
Share

Another option is inheritance. You can have a base class implement a method that can be overridden by child classes. Then you can refer to that base class, and its methods (and pretty much any other member, unlike with interfaces) with more flexibility.

For instance:

 public class SportsballPlayer : $$anonymous$$onoBehaviour
 {
     public float jumpheight = 0.5;
     public virtual void Jump()
     {
          transform.position = transform.position += Vector3.up * jumpheight; 
     }
 }
 
 public class $$anonymous$$ichaelJordan : SportsballPlayer
 {
     public override void Jump()
     {
            base.Jump();
            Debug.Log("Come on and SLA$$anonymous$$");
            
     {
 }

 public class StephenCurry : SportsballPlayer
 {
     public override void Jump()
     {
           transform.position = new Vector3(0, 10000, 0);
     }
 }
 

Then you could assign $$anonymous$$ichaelJordan or StephenCurry to a SportsballPlayer field, and call the jump function on either.

There's pros and cons between interfaces vs. inheritance. Interfaces restrict you to enforcing methods only (no values, etc, though technically you can have methods with return types that force you to provide a desired value) but you can also have multiple interfaces on the same object. With inheritance, you couldn't have $$anonymous$$ichaelJordan inherit from any other class but SportsballPlayer, which can be a pretty big design constraint if you rely on them too much, but also let you reuse code a lot from its base classes - imagine if the base version of Jump had a lot of intricate logic. There's other pros and cons but that's the gist.

avatar image jonjuu Fragsteel · Feb 26, 2020 at 12:49 PM 0
Share

Your solution is also interesting, but no more than your explanation about the advantages and disadvantages of using interfaces or inheritance Thanks to you too.

avatar image jonjuu · Feb 26, 2020 at 11:46 AM 0
Share

Your approach is just what I was looking for. I am ashamed that I didn't realize that I could use the interfaces to implement it after having spent several hours thinking about it . I guess yesterday I had spent too much time working. Thanks a lot.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Cleaning project? After uninstalling an asset 1 Answer

Noob q: how to access functions in script that is in asset folder/view? 1 Answer

Create Unity App that read c# files ? 0 Answers

What is the best way to create this Scoresystem ? 1 Answer

,Getting a Spawner Manager Script to reference a Spawner 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges