• 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
Question by castor · Jul 05, 2013 at 03:41 AM · variablefunctionintpass

Pass a function(float) as variable

So I have this simple function:

 function FadeIn(speed : float){
     fadeSpeed = speed;
     fadeDir = -1;
 }

and I want to add it to a class by doing something like this:

 //The class type
 class ScriptStep {
     var subScript : List.<ScriptStep>;
     var stepHoldLength : float;
     var stepTransform : Transform;
     var audioClip : AudioClip;
     var textLine : String;
     var stepFade : function(float);
 
 //And the function
 function AddStep (currentScript : List.<ScriptStep>, stepFade : function(float)){
     var step = new ScriptStep ();
         step.stepFade = stepFade;
     currentScript.Add(step);
 }


But whenever I try to actually run it:

 AddStep (gameIntro, dialogInteractions.FadeIn(0));

I keep getting the error:

 No appropriate version of 'Dialog_Manager.AddStep' for the argument list '(System.Collections.Generic.List., void)' was found.

What am I doing wrong exactly? A bit new to javascript and just started to learn how to declare variables of the type function...

Any help very appreciated!

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Eric5h5 · Jul 07, 2013 at 01:59 AM

Your code should be:

 AddStep (gameIntro, dialogInteractions.FadeIn);

You can't pass in an argument when you're adding a function as a variable. You can only use an argument when you actually call the function, such as

 step.stepFade(0);
Comment
castor

People who like this

1 Show 6 · 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 castor · Jul 07, 2013 at 02:02 AM 0
Share

Gotcha!! Its working now :) Thanks.

avatar image Eric5h5 · Jul 07, 2013 at 02:12 AM 0
Share

By the way, if you want to be 100% explicit then you can do

 var stepFade : function(float):void;

in order to specify the return value of the function. It's void by default so it works without it, but anyway.

avatar image castor · Jul 07, 2013 at 02:16 AM 0
Share

BTW, what is the right way to declare the variable for the function?

 var action : Function;
 var action : function ();
avatar image castor · Jul 07, 2013 at 02:49 AM 0
Share

Ok...it still doesnt seem to work... this is what I have now:

 var stepActionFloat : function(float):void;
 
 function AddStep (currentScript : List.<ScriptStep>, stepAction : function (float) : void, duration : float){
     var step = new ScriptStep ();
         step.stepActionFloat = stepAction(duration);
     currentScript.Add(step);
 }
 

and I get a

 BCE0022: Cannot convert 'void' to 'function(): void'.
 
avatar image castor · Jul 07, 2013 at 02:56 AM 0
Share

Is it because I'm adding it as a variable again? I think I understand...Only when I call it! Super clear answer.

Show more comments
avatar image

Answer by umangindianic · Jul 05, 2013 at 04:46 AM

Use of file in top of the script.

using System.Collection.Generic;

this is use for the List which is used by you in your script.

List can not add any type of whole function but it can add the value of function. So, make your function with its return type.

Comment
Eric5h5

People who like this

-1 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 castor · Jul 07, 2013 at 01:35 AM 0
Share

You mean import System.Collections.Generic; right?

I'm not sure I understand what you mean by "List can not add any type of whole function but it can add the value of function." The function that I want to use (FadeIn) doesn't return anything, it just does a couple of steps, what should I ask for it to return?

avatar image castor · Jul 07, 2013 at 01:49 AM 0
Share

I noticed that I can make it work if the function doesnt pass a value. For example if I have this function :

 function UnPauseGame (){
     worldisUnpausedTime = Time.time;
     worldIsPaused = false;
 }

I would also get an error, but if I add a "yield to the end of it:

     function UnPauseGame (){
         worldisUnpausedTime = Time.time;
         worldIsPaused = false;
         
         yield;
     }

I can now do this: AddStep (knowsAboutJournal01, worldInteractions.UnPauseGame);

I noticed the function ends up changing from being a function () : void to function () : IEnumerator but I don't understand what that means and how it makes it acceptable to now be used.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

How to pass a int from a object to another 1 Answer

passing a javascript array into a function sorts it? 1 Answer

Building a 3D Neural Network for Bot AI? 1 Answer

How to use same code at multiple scenes ? 1 Answer

Function as parameter? 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