Modyfing survival shooter script to add health
Im adjusting the survival shooter health script so that my pick up item adds health. There is a parsing error and some unexpected symbols but im sure its because it says that "public" in take damage and "void" in death is an unexpected symbol in one case and in the other it says it cannot be used in this context. Also it says that "Death ()" in Take Damage doesnt exist in the current context, please help!
Player Health:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public int maxHealth = 100;
public Slider healthSlider;
public Image damageImage;
public AudioClip deathClip;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
Animator anim;
AudioSource playerAudio;
PlayerMovement playerMovement;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
void Awake ()
{
anim = GetComponent <Animator> ();
playerAudio = GetComponent <AudioSource> ();
playerMovement = GetComponent <PlayerMovement> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
currentHealth = startingHealth;
}
void Start ()
{
startingHealth = maxHealth;
}
void Update ()
{
healthSlider.value = currentHealth;
if(damaged)
{
damageImage.color = flashColour;
}
else
{
damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
}
damaged = false;
}
public void AddHealth (int amount)
{
currentHealth += amount;
if (currentHealth <= 0)
{
currentHealth = 0;
{
if (currentHealth >= maxHealth)
{
currentHealth = maxHealth;
}
}
public void TakeDamage (int amount)
{
damaged = true;
currentHealth -= amount;
playerAudio.Play ();
if (currentHealth <= 0 && !isDead)
{
Death ();
}
}
void Death ()
{
isDead = true;
playerShooting.DisableEffects ();
anim.SetTrigger ("Die");
playerAudio.clip = deathClip;
playerAudio.Play ();
playerMovement.enabled = false;
playerShooting.enabled = false;
}
public void RestartLevel ()
{
Application.LoadLevel (Application.loadedLevel);
}
}
` There are no errors on my pick up object but in case it is affecting my player health script i decided to post it up.
PlayerAddhealth:
using UnityEngine;
using System.Collections;
public class PlayerAddHealth : MonoBehaviour
{
public int addHealth = 10;
public int startingHealth = 100;
public int currentHealth;
GameObject healthpickup;
GameObject player;
PlayerHealth playerHealth;
void Awake ()
{
healthpickup = GameObject.FindGameObjectWithTag ("healthpickup");
playerHealth = player.GetComponent<PlayerHealth> ();
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == healthpickup)
{
playerHealth.AddHealth (addHealth);
Destroy (healthpickup);
}
}
}
Is this a direct copy and paste? I can't see any errors. Your description does seem like a scope issue that occurs around TakeDamage.
Scope is the existence of variables and where they are available.
For example if you declare a variable inside a function
public int y = 0;
public void $$anonymous$$yFunc()
{
int x = 0;
}
int x can not be accessed outside the function. It doesn't exist for all intents and purposes, except inside the function. I guess you could say its private to that function. You will notice that if you try to declare int x as public it will complain at you. This is because a variable inside a function can not be public, for the reason I just described.
int y can be accessed anywhere in the class as its a class member. A global or class variable, defined in the class root, can exist anywhere in the script whether private or public. To the class these tags indicate whether the field is visible or not to external objects.
Can you please post your new scripts for the coding, I would like to use it if I can?
Answer by meat5000 · Dec 02, 2015 at 08:40 PM
Start with making all variables and functions public.
If something isnt decidedly public or private, in C# it defaults to private. Private members are only available to the class itself and can not be accessed outside the class hence
doesnt exist in the current context
Spotted an error:
if (currentHealth <= 0)
{
currentHealth = 0;
{ // <- Bracket the wrong way round.
If that doesnt work, post the errors that appear in your console including line numbers.
It did fix my Death () error but now it says public is an unexpected symbol
(73,14): error CS1525: Unexpected symbol 'public'
(88,14): error CS1525: Unexpected symbol 'public'
Yes it's fixed my Player health script but now there says on my PlayerAddHealth that
(24,37): error CS0019: Operator '==' cannot be applied to operands of type 'strong' and 'UnityEngine.GameObject'
Wait Never$$anonymous$$d I figure it out but thank you so much you guys. You are beautiful tropical fish.
A strange but awesome compliment :)
Glad to help.
Your answer
Follow this Question
Related Questions
Parsing error and unexpected symbol 1 Answer
Parser Error - Unexpected symbol 2 Answers
unexpected symbols 1 Answer
Error CS1525|Unexpected " { " Please Help. Urgent. 1 Answer
C# - Unexpected Symbol ERROR? 1 Answer