- Home /
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.
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").
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.
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
Follow this Question
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