Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by sfabian98 · Dec 02, 2015 at 10:05 AM · parsing errorunexpected-symbol

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);
         }
     }
 }


Comment
Add comment · Show 4
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 Guppie1337 · Dec 02, 2015 at 08:15 PM 0
Share

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.

avatar image sfabian98 · Dec 02, 2015 at 08:32 PM 0
Share

Scope issue? Sorry I'm new at this. @Guppie1337

avatar image meat5000 ♦ sfabian98 · Dec 02, 2015 at 08:49 PM 0
Share

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.

avatar image Combatsergion · Mar 15, 2017 at 02:43 PM 0
Share

Can you please post your new scripts for the coding, I would like to use it if I can?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 7 · 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 meat5000 ♦ · Dec 02, 2015 at 08:40 PM 0
Share

If that doesnt work, post the errors that appear in your console including line numbers.

avatar image sfabian98 · Dec 02, 2015 at 09:27 PM 0
Share

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'

avatar image meat5000 ♦ sfabian98 · Dec 02, 2015 at 09:33 PM 0
Share

Spotted your error. Added to answer.

avatar image Guppie1337 meat5000 ♦ · Dec 03, 2015 at 04:56 PM 0
Share

nice catch

avatar image sfabian98 · Dec 03, 2015 at 08:59 PM 0
Share

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'

avatar image sfabian98 · Dec 03, 2015 at 09:39 PM 1
Share

Wait Never$$anonymous$$d I figure it out but thank you so much you guys. You are beautiful tropical fish.

avatar image meat5000 ♦ sfabian98 · Dec 04, 2015 at 09:03 AM 0
Share

A strange but awesome compliment :)

Glad to help.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


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