- Home /
Cannot assign to 'Respawn' because it is a 'method group'?
Hello again, I have yet another problem with the health script I have been trying to write, First off here is my code:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
{
public float maxHealth = 100.0f ;
public float curHealth = 100.0f ;
public GameObject Player;
void Start(){
}
void Update(){
if(curHealth < 0){
curHealth = 0;
}
if(curHealth > maxHealth){
curHealth = maxHealth ;
}
if(Respawn = true){
ApplyDamage(-100.0f);
}
}
void OnGUI(){
GUI.Label(new Rect(100,100,100,100), curHealth + "/" + maxHealth);
}
void Respawn(bool boo){
Instantiate(Player, new Vector3 (23.21657f, 3.257863f, -0.6684538f), Quaternion.identity);
Destroy(Player);
}
void ApplyDamage(float damage){
curHealth -= damage ;
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Zombie")){
ApplyDamage(10);
if(curHealth <= 0.0f){
//Instantiate(Player, new Vector3 (23.21657f, 3.257863f, -0.6684538f), Quaternion.identity);
//Destroy(Player);
Respawn(true);
}
}
}
}
Ok so, I am currently able to respawn with this code but my health stays at 0, I tried using curHealth = 100; in the Respawn but it doesn't work, I also tried using ApplyDamage(-100.0f) in it but neither worked, so I went on to add an if statement (currently in the code) to set the health if Respawn = true but i get the error above.
Can someone explain what the error means and possibly help me with a solution to setting my health after respawn?
Your compiler issue is because comparison use double-equal signs like this: '==', so your code should be:
if(Respawn == true){
ApplyDamage(-100.0f);
I don't get how you are doing respawning. Where is this script attached? Do you expect the health of the newly spawned Player to get reset to 100?
After that, you're asking if a void is returning as true. Can't do that. $$anonymous$$ake it a public bool and return a bool value.
@robertbu I had already tried ==, but it then says "Operator '==' cannot be applied to operands of type '$$anonymous$$ethod group' and 'bool'", As far as how I expect it to work, It should create a copy of the player, then destroy the original and set the health of the copy to 100.
@tw1st3d If i make Respawn a public bool then the compiler complains on the if statement that it "Cannot assign to 'Respawn' because it is a 'method group'" still, then in Respawn it complains that "'Health.Respawn(bool)': not all codepaths return a value"
Answer by TutiBueno2 · Aug 01, 2013 at 04:11 PM
OK. Line 26 should be:
if(Respawn == true)
If Respawn method is a void, that's another problem. One of the problem was comparing with single '='.
I really don't see why you have to destroy the player and instantiate it again. You could just change the player position to the one that is hard-coded in your Respawn method. I'll post another code for you.
Answer by Xtro · Aug 01, 2013 at 04:04 PM
Your "Respawn" is a method and its return type is void. You can't test it in if block which requires a bool value.
Your answer

Follow this Question
Related Questions
turn on off particle system 1 Answer
Hide the ImageTraget in Real world 0 Answers
how to get in game numerical input 1 Answer
How to Avoid Corner paths in A* pathfinding 1 Answer
only detect numerical keyboard input 2 Answers