• 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
0
Question by meneliki3 · Feb 22, 2020 at 03:29 PM · unassiunassignedreferenceexception

UnassignedReferenceException - but it IS assigned, and the code works!

Hi,

Having a strange issue here. When I run my code, it works - I'm manipulating a tilemap's tiles. However, the console gives me the error in the title indicating that I'm missing a reference to the object. It's the very object I'm manipulating (it works), so if the reference wasn't there, the effect wouldn't happen (which it does).

I've got 4 tilemaps on a grid. All of these tilemaps are referenced in the same place, on the same script, in the same manner:

     public Tilemap flatHexTilemap;  
     public Tilemap flatHexTilemapLevel2;
     public Tilemap flatHexTilemapLevel3;
     public Tilemap Fog;

Further down in my "UpdateLineOfSight()" method, I'm referencing 2 of those tilemaps:

 if (flatHexTilemapLevel2.HasTile(localPlace))  //no error reference here
             {                
                 Fog.SetTile(localPlace, null);   //error points to this line here
 
                 print(flatHexTilemapLevel2.GetTile(localPlace).name + " ");   //again no error here
                 print("X: " + localPlace.x.ToString());
                 print("Y: " + localPlace.y.ToString());
             }

All 4 of those tilemaps are linked to the script (dragged in the inspector) from the same spot in the heirarchy to the same spot on the script, in the same manner. However, ONE of those tilemaps is generating an error, despite the code executing properly (it's basically just poking holes in the fog of war map wherever there happens to be tiles on the level 2 map. Not ultimately what i need, but just for testing.)

The error says the Fog variable is missing the object reference. But.. it really isn't.. lol.

Any ideas? This one is really weird.

Thanks

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 sacredgeometry · Feb 22, 2020 at 03:36 PM 0
Share

Where is flatHexTilemapLevel2 assigned?

And what is the exact error message?

avatar image meneliki3 sacredgeometry · Feb 22, 2020 at 03:39 PM 0
Share

It's assigned on my player$$anonymous$$ovement script. The error is: "UnassignedReferenceException: the variable Fog of player$$anonymous$$ovement has not been assigned. You probably need to assign the Fog variable of the player$$anonymous$$ovement script in the inspector."

avatar image sacredgeometry meneliki3 · Feb 22, 2020 at 03:41 PM 0
Share

It will tell you exactly what character and line the exception was thrown from. I was asking for the full exception for that reason.

Or a stack trace would do the same job.

Show more comments

1 Reply

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

Answer by sacredgeometry · Feb 22, 2020 at 03:43 PM

You may be setting it but you are probably accessing it before its set and thats why the exception is being thrown. Make sure to set it first.

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 meneliki3 · Feb 22, 2020 at 05:35 PM 1
Share

Yeeaaaaaaa I'm a moron.

All this time, I had that same script accidentally attached to a SECOND object... in THAT object, the reference wasn't dragged over. This also explains why the cellBounds.allPositionsWithin was returning twice as many tiles as it should've been and tanking performance.

Thanks for the help. I do appreciate it! :)

avatar image sacredgeometry meneliki3 · Feb 22, 2020 at 06:38 PM 0
Share

Happens to the best of us.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

120 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

Related Questions

Unassigned Reference Exception. How to create and add a texture 2D in the inspector? 0 Answers

UnassignedReferenceException: The variable has not been assigned. 1 Answer

UnassignedReferenceException: The variable target of New Cylinder has not been assigned. You probably need to assign the target variable of the New Cylinder script in the inspector. 0 Answers

Unassigned Reference Exception 1 Answer

Unassigned reference exception after rewarded ad 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges