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
1
Question by FH_88 · Apr 01, 2014 at 03:16 PM · javascripterrorfunctionaccess

Similar scripts but error on one and not the other

I have a script with two functions in, then i have two other scripts, where i am trying to access these two functions in. The first script Works fine and it is calling the function, but in the other script i have called the function in the same way as the first one, but i get the error " Invalid field type 'void' " why?

Here is the script with the functions in.

 //EScript.js
                 
                 static function AddL() {
                 if (lives < maxLives)
                 {
                     lives++;
                     PlayerPrefs.SetInt("lives", lives);
                 }
             }         
         
             static function SubL(){
                 if(lives <= 0)
                     return false;
                 if (lives == maxLives)
                 {
                     nextLife = UnixTimestamp() + RegenInterval;
                 }
                 PlayerPrefs.SetInt("nextLife", nextLife);
                 lives--;
                 PlayerPrefs.SetInt("lives", lives);
                 return true;
             }

 

 

Here is the first "access script" this one works.

 var subfunct = EScript.SubL();
         
         function OnGUI() {
                 
                 
                 if (GUI.Button(Rect(100,100,120,40),"Level 1")){
                         EScript.SubL();
                         Application.LoadLevel("leveltimetestscerne");    
                     }        
         }
 
 

And here is the second "access" script this one doesn't work.

  var addfunct = EScript.AddL();   // this is the line where the error is
     
     function OnTriggerEnter(other : Collider) {
      
         if (other.gameObject.tag == "Player") 
         {  
             EScript.AddL();    
          } 
     }
Comment
Add comment · Show 7
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 perchik · Apr 01, 2014 at 03:25 PM 0
Share

Interesting question but I really want to downvote because of the use of the word 'codes' to describe script files.

avatar image FH_88 · Apr 01, 2014 at 03:31 PM 1
Share

Thanks for your respond, i have now edited the title :)

avatar image thornekey · Apr 01, 2014 at 04:10 PM 0
Share

which line does it say is causing the error?

avatar image FH_88 · Apr 01, 2014 at 04:16 PM 1
Share

I have written it in the last script as a comment.

avatar image perchik · Apr 01, 2014 at 04:18 PM 1
Share

Is there something more you're actually doing? I've found sometimes when I simplify my problem to post it, I end up removing the actual error...

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hamstar · Apr 02, 2014 at 07:02 AM

SubL returns a value, but AddL does not. EScript.AddL() executes the function, but nothing is returned, that is why you can't assign the return value to addfunct.

I imagine one of two things is happening. Either you forgot to put return values into AddL(), or you are trying to use addfunct as a delegate.

A: If you forgot to put the return statements in:

 // perhaps you wanted something like this?
 static function AddL() {
     if (lives < maxLives)
     {
         lives++;
         PlayerPrefs.SetInt("lives", lives);
         return true; // success
     }
     return false; // failure
 }   

   

B: If you wanted to use delegates:

Your syntax would be incorrect. You should remove the brackets in your delegate assignment var addfunct = EScript.AddL, and then use the delegate with the delegate variable name addfunct().

Second script using a delegate:

 var addfunct = EScript.AddL;
 
 function OnTriggerEnter(other : Collider) 
 {
     if (other.gameObject.tag == "Player") 
     {  
         addfunct();    
     } 
 }
Comment
Add comment · 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

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

23 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

Related Questions

Access function (and variable) outside class in the class in Javascript. 1 Answer

Error: expecting ( 1 Answer

Incremental game need help 1 Answer

How to start a function from another scene. 1 Answer

Script is causing immense lag, and I don't know what's causing it. 2 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