Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GrKl · Jan 06, 2014 at 01:27 PM · c#functioncopy

c# - copy a function in another?

Hi all,

Beeing new to programming, this question might be stupid... Sorry if it is.

I wanted to know if (and how) it would be possible to copy a function script to another one.

example of what I would like to do:

in script 'Attacks':

 void attack1()
 {
 }
 
 void attack2()
 {
 }
 
 void OnGUI()
 {
 if (GUI.button(new Rect(0,0,100,100),"att01"))
 {
 attack1() = gameObject.GetComponent<Warrior>().att01();
 }
 }

the function 'att01()' would thus be included in the script 'Warrior' attached to the same GameObject.

Is something like this possible? How?

Thanks for your help

Comment
Add comment
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

2 Replies

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

Answer by GameVortex · Jan 06, 2014 at 01:46 PM

You can sort of do that with events and delegates. Something like this:

 private delegate void CopiedFunction();
 private CopiedFunction copiedFunction;
 
 private void Start()
 {
     copiedFunction = FunctionToCopy;
 
     //using the function:
     copiedFunction();
 }
 
 private void FunctionToCopy()
 {
    
 }

Keep in mind that the function to copy needs to match the signature of the delegate (the return value and the parameters needs to be the same).

**Here** you can read more about the use of delegates.

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 GrKl · Jan 07, 2014 at 09:09 PM 0
Share

Perfect thanks, it works (of course ;) ) simple & verry usefull to me

avatar image
1

Answer by Bunny83 · Jan 06, 2014 at 01:44 PM

You can't "copy" a function. C# is not a dynamic scripting language. Every function / method is compiled. However what you want is called a delegate. A delegate is like a "function pointer". It allows you to assign a function with the same signature to a delegate variable. Then you can use the variable to execute the assigned function:

You can define a delegate type like this:

     public delegate RETURNTYPE MyDelegateType(PARAMETERS);

There is a predefined type "Action" which equals:

     public delegate void Action();

To define a delegate variable you simply use the delegate as type:

     public MyDelegateType variableName;

So in your case you can do:

 public System.Action attack1;
 
 void OnGUI()
 {
     if (GUI.button(new Rect(0,0,100,100),"att01"))
     {
         attack1 = gameObject.GetComponent<Warrior>().att01;
     }
 }

If you want to execute the delegate just do this:

     attack1();
Comment
Add comment · Show 2 · 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 GrKl · Jan 06, 2014 at 02:01 PM 0
Share

great thanks, i'll try this this evening. Is this a heavy from a ressource point of view?

avatar image GrKl · Jan 07, 2014 at 09:10 PM 0
Share

Thanks, I just had to declare it as GameVortex wrote bellow, I probably miss typed something as with yours I was not able to declare the 'att01'

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getting a Non Static Variable From a Different C# Script 2 Answers

Algorithm for loading sounds 1 Answer

Sending a message from javascript to C# 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