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 MiniGrief · Sep 13, 2014 at 09:29 PM · c#-and-js

error CS1520: Class, struct, or interface method must have a return type

I have tried to look at other questions but can't work this out. I get the error "error CS1520: Class, struct, or interface method must have a return type". The script is called 'Health'.

 using UnityEngine;
 using System.Collections;
 
 public class Health : MonoBehaviour {
 
     public float hitPoints = 100f;
     float currentHitPoints;
 
     // Use this for initialization
     void Start () {
         currentHitPoints = hitPoints;
     }
 
     [RPC]
     public void TakeDamage(float amt) {
         currentHitPoints -= amt;
 
         if(currentHitPoints <= 0) {
             Die();
         }
     }
 
     /*void OnGUI() {
         if( GetComponent<PhotonView>().isMine && gameObject.tag == "Player" ) {
             if( GUI.Button(new Rect (Screen.width-100, 0, 100, 40), "Suicide!") ) {
                 Die ();
             }
         }
     }*/
 OnGUI()
     {
         //Icons For GUI
         GUI.Box(new Rect(5,30,40,20), "Hp");
         
         //Health Main Bar
         GUI.Box(new Rect(45, 30, barLength, 20), currentHitPoints.ToString("0"));
     }
 
     void Die() {
         if( GetComponent<PhotonView>().instantiationId==0 ) {
             Destroy(gameObject);
         }
         else {
             if( GetComponent<PhotonView>().isMine ) {
                 if( gameObject.tag == "Player" ) {        // This is my actual PLAYER object, then initiate the respawn process
                     NetworkManager nm = GameObject.FindObjectOfType<NetworkManager>();
 
                     nm.standbyCamera.SetActive(true);
                     nm.respawnTimer = 3f;
                 }
 
                 PhotonNetwork.Destroy(gameObject);
             }
         }
     }
 }
 

I know it's to do with the OnGUI thing as that was copied from javascript as I need to try get a HP GUI working.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by rutter · Sep 13, 2014 at 10:05 PM

This line:

 OnGUI()

Should be:

 void OnGUI()

You need to explicitly declare a return type in C#, even if there is no return value (which we call "void").

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 MiniGrief · Sep 14, 2014 at 10:35 AM 0
Share

That helps, But causes lots more errors.

 Error CS0103: The name 'barLength' does not exist in the current context
 Error CS1502: The best overloaded method match for `UnityEngine.Rect.Rect(float, float, float, float)' has some invalid arguments
 Error CS1503: Argument `#3' cannot convert `object' expression to type `float'
 Error CS1502: The best overloaded method match for `UnityEngine.GUI.Box(UnityEngine.Rect, string)' has some invalid arguments
 Error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Rect'

Thanks for the help with the first error.

avatar image alishka · Sep 14, 2014 at 10:56 AM 0
Share

Hello !

First two errors are caused by the barLength variable; it was never declared in your code.

The other errors are caused by currentHitPoints.ToString("0")), should be .toString(), since formatted .toString(string argument) returns an object, not a string.

Cheers !

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

error CS1520: Class, struct, or interface method must have a return type 2 Answers

Does c# scripts take up more space than javascript scripts? 1 Answer

Accessing JavaScript variable from C# and vice versa. 3 Answers

C# equivalent of a while loop outside of functions? 0 Answers

help script error 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