- Home /
Problem with raycasting when checking neighbouring objects?
Hello, I am creating a game that looks like this: http://gyazo.com/366aedddefb336d7052dec5d35876368
And I have a method called VerifyPlacement which takes a tile, and checks the four surrounding tiles to see if it is just a tile, or if it has one of those colorful symbols on it(in other words, it gets the objects that are above, below, to the left and to the right of the tile).
I am trying to do this by raycasting above each of the surrounding tiles, like so:
Vector2 checkHere = new Vector2 (c.x + s.x, c.y); //"c" is the collider, and "s" is the size of the collider. so in this case, it should cast a ray exactly one tile to the right.
RaycastHit2D hit = Physics2D.Raycast(checkHere, Vector2.zero);
Debug.Log (hit.transform.gameObject);
However, I get an error message on the debug.log which is:
NullReferenceException: Object reference not set to an instance of an object TileController.VerifyPlacement (UnityEngine.GameObject obj) (at Assets/Scripts/TileController.cs:41)
Is there a simple way to fix this problem and get my raycasting to work? Thanks for the help.
We really need to see the entire TileController.cs file.
The problem is often not where you think it is...
Just like Jeff said we need to see more to resolve that raycast problem...
But I don't think you're using the good approach to detect if the "neighbors" of a tile have a symbol. I suggest you to store the position and the symbol in a script on each on your tile and then store all your tiles in a multi array. Then it will be really easy to check the four ones surrounding your tile.
True, most puzzle gems are easiest to implement with the idea of a "game board" as an in-memory data structure.
Your answer
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
Problem with raycast distance 1 Answer
how to select an object with raycasting 1 Answer
[SOLVED] Raycast for custom button JS 1 Answer
What's wrong with my RaycastHit2D? 1 Answer