- Home /
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();    
          } 
     }
Interesting question but I really want to downvote because of the use of the word 'codes' to describe script files.
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...
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();    
     } 
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                