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 ProgrammeJava · Dec 20, 2016 at 05:33 PM · javascriptscripting problemvariablescript errorunknown-identifier

Unknown Identifier : Hunger?

Hello! I am very sorry that I am probably asking a very basic question. I am not that good at scripting in Unity. I am trying to create a script that will display when hunger = 0, the game restarts. This is my script that I want to fix:

 #pragma strict
 
 function Start () {
     var Hunger = 100;
 }
 
 function Update () {
     
 }
 function OnGUI()
 {
   GUI.Label(Rect(0,0,100,100),"Hunger: "+Hunger);
 }
 function onHungerDeath() 
 {
   if (Hunger <= 0) {

   Application.LoadLevel(0);
                             
     }
 }

Here is the error that is displayed:

  Assets/Scripts/variabledisplay.js(12,42): BCE0005: Unknown identifier: 'Hunger'.
  Assets/Scripts/variabledisplay.js(16,7): BCE0005: Unknown identifier: 'Hunger'.

Thank you for taking your team to help me.

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
0
Best Answer

Answer by Landern · Dec 20, 2016 at 06:20 PM

You need to declare Hunger outside of the scope of a function, in the function it is not available and this doesn't work like javascript and it's closure.

Update your script like so:

 #pragma strict
 
 var Hunger: int;
 
  function Start () {
     Hunger = 100;
  }
  
  function Update () {
      
  }

  function OnGUI() {
    GUI.Label(Rect(0,0,100,100),"Hunger: "+Hunger);
  }

 function onHungerDeath() {
    if (Hunger <= 0) {
        Application.LoadLevel(0);
    }
 }


Comment
Add comment · Show 4 · 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 ProgrammeJava · Dec 21, 2016 at 01:40 AM 0
Share

@Landern, thanks for your response, however 3 more errors pop up when I copied this in to my unity:

 Assets/Scripts/variabledisplay.js(12,7): BCE0005: $$anonymous$$ identifier: 'Hunger'.
 Assets/Scripts/Shop.cs(3,0): error CS1525: Unexpected symbol `var'
avatar image Landern ProgrammeJava · Dec 21, 2016 at 02:06 PM 0
Share

Assets/Scripts/variabledisplay.js(12,7): BCE0005: $$anonymous$$ identifier: 'Hunger'.

This doesn't make sense if the variable is accessible to the entire class/.js file, make sure the variable is specified as above.

In the case of

Assets/Scripts/Shop.cs(3,0): error CS1525: Unexpected symbol `var'

If you put the same code into a c# class(.cs extension) then it can not infer the type with no value, in the case of the .cs just use something like:

 public int Hunger;

or

 public int Hunger = 100; // you can initialize the value during declaration.

The above c# declaration can be applied to the .js class as well.

 var Hunger : int = 100; // you can then remove the initial set of Hunger from the Start function.





avatar image ProgrammeJava · Dec 21, 2016 at 05:19 PM 0
Share

@Landern, thanks for your response. It turns out that I was using C# to program this ins$$anonymous$$d of Javascript. (I feel so embarrassed, lol.) If it wasn't for you responding and taking your time, I would've given up on this project! Thanks.

avatar image KingFerretDino ProgrammeJava · Jan 19, 2017 at 03:29 PM 0
Share

$$anonymous$$eep your scripts in the same lingo m8.

JS FOR LIFE!

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

132 People are following this question.

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

Related Questions

What's wrong with my script? 2 Answers

JavaScript Problem - BCE0043 - Unexpected Token Help 0 Answers

Game development general question(Multiplayer - Singleplayer) 1 Answer

Unity {[(2D)]} RotateAround while jumping 0 Answers

i need help with my board manager coding ASAP 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