Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jan 29, 2015 at 02:39 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Digital-Phantom · Jan 29, 2015 at 02:23 PM · javascriptvariableerror messagepubliccompile

Why can't I make this VAR public ? (Solved)

I've been using this script from the unity tutorials which works fine as is.

 // Print the name of the closest enemy
 print(FindClosestResource().name); 
     
 // Find the name of the closest enemy
 function FindClosestResource () : GameObject
 {
 
 // Find all game objects with tag Enemy
 var gos : GameObject[];
 gos = GameObject.FindGameObjectsWithTag("Resource");
  
 var closest : GameObject; 
 var distance = Mathf.Infinity; 
 var position = transform.position; 
 
 // Iterate through them and find the closest one
 for (var go : GameObject in gos)
 { 
 var diff = (go.transform.position - position);
 var curDistance = diff.sqrMagnitude; 
 if (curDistance < distance)
 { 
     closest = go; 
     distance = curDistance; 
 }
  
 } 
 return closest;    
 }

I was trying to make one of the variables public but for some reason it gives me an error. The line/variable is -

 var gos : GameObject[];

But when I change that to

 public var gos : GameObject[];
 

I get these error messages-

Assets/AI Scripts/Find Closest Object.js(9,1): BCE0044: expecting }, found 'public'.

Assets/AI Scripts/Find Closest Object.js(29,1): BCE0044: expecting EOF, found '}'.

I don't understand why it suddenly is asking for { when all I've done is tried to change the variable type?

Can somebody explain this please ?

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

3 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by meat5000 · Jan 29, 2015 at 02:30 PM

When you declare a variable inside a function, it's scope is limited to that function. Declare it outside the function.

Comment
Add comment · Show 2 · 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 Digital-Phantom · Jan 29, 2015 at 02:36 PM 0
Share

I just tried that and (as you already know it works) I moved the line/variable in question to the start of the script and all is fine. Will admit though it seems strange to me that it works like that and not work just because I added the 'public' preface !

Anyway, thanks very much for the reply, much appreciated

:)

avatar image meat5000 ♦ · Jan 29, 2015 at 03:29 PM 0
Share

Sometimes the simplest explanation is the most appropriate :)

avatar image
2

Answer by HarshadK · Jan 29, 2015 at 02:36 PM

The reason behind this is that the variable 'gos' is declared inside the function FindClosestResource() which means that the scope of this variable is limited till the function is executing. Once the function is done executing that variable will be removed hence you can not make it a public variable. Since each time you enter the FindClosestResource() function that variable will be declared again and will be processed inside that function itself and will be removed once the function stops processing. So it is a local variable to that function itself so you can not make it public.

If you really want to make that variable public you can declare it outside that function like:

  // Now this is out of function and can be made public
  public var gos : GameObject[];
      
  // Find the name of the closest enemy
  function FindClosestResource () : GameObject
  {
  // Find all game objects with tag Enemy
  gos = GameObject.FindGameObjectsWithTag("Resource");

  
Comment
Add comment · Show 1 · 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 Digital-Phantom · Jan 29, 2015 at 02:38 PM 0
Share

@Harshad$$anonymous$$ thanks

avatar image
1

Answer by NoseKills · Jan 29, 2015 at 02:37 PM

You can not declare anything public inside functions. If you declare a variable inside a function, it will only be created when you call that function and it will be lost when you exit the function.

It wouldn't make sense to have a variable like that be public so you can access it from other scripts.

If you need a variable to "stay alive", you just have to take it out of the function to make it a "field".

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to change a variable in the same script? 3 Answers

Array in JS NOT Working with Components?! 2 Answers

Do something when something happens in another script. 3 Answers

When i call some variables to an other second script i have a problem 1 Answer

Accessing other script problem 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