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 /
  • Help Room /
avatar image
0
Question by Raynoko · Sep 19, 2015 at 09:04 AM · c#script.functioncomponentparameters

Component as parameter

Hi, im not sure if it does exist, but i dont know how to do somthing like that:

 public void myFunction(Component **script**)
 {
 gameObject.GetComponent<**script**>().function;
 }

If anybody know how to do, pls help me if u can. Thanks

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 Glurth · Sep 19, 2015 at 01:10 PM 0
Share

The "Component" you have in the parameter references an instance of an object of class type "Component", but the parameter in your GetComponent call, takes a CLASS as a parameter, like say.. "< Text$$anonymous$$esh >" or "".
So, It's not quite clear what goal you are trying to achieve here. It looks like what you want, is to be able to select one, of many possible components belonging to a game object. Which component you select, depends on the parameter you are passing. Then call a specific function, that is a member of that component. If so, a question: How do you know that whatever component you select, will actually have a function called "function"?

Have you taken a look at "Scriptable Objects" yet, http://docs.unity3d.com/ScriptReference/ScriptableObject.html? You $$anonymous$$AY want to use this class, rather than component.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by RudyTheDev · Sep 19, 2015 at 02:39 PM

If I understand the intention correctly, which is to call the function on the given type of script, then there are a few ways to do this. Not sure if this is suitable for your exact problem, but an OOP organized way with generics would be something like this:

 using UnityEngine;
 
 public class TestClass : MonoBehaviour
 {
     private void Start()
     {
         gameObject.AddComponent<ExampleFunctionClass1>();
         gameObject.AddComponent<ExampleFunctionClass2>();
 
         DoFunction<ExampleFunctionClass1>();
         DoFunction<ExampleFunctionClass2>();
     }
 
     private void DoFunction<T>() where T : MonoBehaviourWithFunction
     {
         gameObject.GetComponent<T>().Function();
     }
 }
 
 public abstract class MonoBehaviourWithFunction : MonoBehaviour
 {
     public abstract void Function();
 }
 
 public class ExampleFunctionClass1 : MonoBehaviourWithFunction
 {
     public override void Function()
     {
         Debug.Log("Function from 1!");
     }
 }
 
 public class ExampleFunctionClass2 : MonoBehaviourWithFunction
 {
     public override void Function()
     {
         Debug.Log("Function from 2!");
     }
 }
 

This works because C# knows any type derived from MonoBehaviourWithFunction will have the Function() and the DoFunction<T>() knows that you will tell it which type to use.

Comment
Add comment · Show 1 · 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 Glurth · Sep 19, 2015 at 03:15 PM 0
Share

This answers both the OP question, AND the question I asked the OP in my comment. +1

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

28 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

Related Questions

Is this bug known? function parameter s and if statements c# 1 Answer

How to set a bool from a script equal to another bool from another script? 1 Answer

Activating Scripts in another Script? 2 Answers

Network.Instantiate passing parametrs to instantiated object, unassigned reference exception 0 Answers

Instantiate after loading scene 0 Answers


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