• 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 /
avatar image
Question by damandamn · Oct 05, 2015 at 10:33 AM · error message

Global variable "does not exist in current context"

Hi all, I'm newish to unity and C#, but this is the first issue I've run into that has really stumped me. I'm currently writing code to highlight all tiles within a unit's movement range, but I'm getting an error that claims that an array (int[,] tilesHighlighted) does not exist in the context of a function, despite it being declared globally.

 public class rangeHandler : MonoBehaviour
 {
     public tileMap map = new tileMap();
     public tileData td = new tileData();
 
     int[,] tilesHighlighted;    
 
     public void showRange(int x, int y, int r)
     {
         tilesHighlighted[map.graph[x, y].x, map.graph[x,y].y]
                         = map.tileAtNode(map.graph[x, y].x, map.graph[x, y].y);
 
 
         foreach (Node n in map.graph[x, y].edges)
         {
             if (tilesHighlighted(n.x,n.y) == null)
             {
 
 
                 tilesHightlighted[n.x, n.y] = map.tileAtNode[n.x, n.y];
 
             }
         }
         r--;
     }
 

Everywhere past "foreach", it's giving me an error. Its first usage at the beginning of showRange() is fine. Any ideas?

Comment

People who like this

0 Show 3
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 ♦ · Oct 05, 2015 at 10:32 AM 1
Share

int[,] tilesHighlighted; remains uninitialised. Look at the two above it.

avatar image Bonfire-Boy · Oct 05, 2015 at 10:42 AM 1
Share

There is another issue in the code - line 20 where you have 'tilesHightlighted' instead of 'tilesHighlighted'.

avatar image meat5000 ♦ Bonfire-Boy · Oct 05, 2015 at 10:50 AM 0
Share

Well spotted

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Suddoha · Oct 05, 2015 at 10:51 AM

The line that causes this is

  if (tilesHighlighted(n.x,n.y) == null)

because you use parenthesis here, you probably wanted to use [n.x,n.y] instead of (n.x,n.y).

Also, you'll probably get NullReferenceExceptions because the script does not populate the array unless you do that through another script already.

Comment
Bunny83

People who like this

1 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 Bunny83 · Oct 05, 2015 at 12:18 PM 1
Share

Well, analyzed. And to repeat what Bonfire said there's a typing error in line 20 as well.

avatar image Bonfire-Boy · Oct 05, 2015 at 12:36 PM 1
Share

Yep, good spot.

Not related to the error, but since the OP is new to C# it may also be worth pointing out that the r-- at the end of showRange() has no impact, since it's only changing a local variable that is discarded immediately.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

apk unable to install see console ive tried everything please help am really frustrated 1 Answer

Unity 5.6.1 Causes Error When Using System.Lazy 1 Answer

SceneManagement error unity 2017 0 Answers

How to fix error cs1518 1 Answer

how do I solve 'VR: OpenVR Error! OpenVR failed initialization with error code VRInitError_Init_PathRegistryNotFound: "Installation path could not be located (110)"!' 4 Answers


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