- Home /
Strange Raycast Problem
I am trying to make a hexagonal movement system like in for the king, so I am casting a ray into the direction of my target until it hits another hexagon, but my while loop won't run because unity says that my RaycastHit hitTwo does not have a value. Shouldn't it work even if hitTwo is null?
FYI targetHex is the gameObject that I want to travel to, layermask is my layermask, currentHex is the gameObject that I am currently at, and otherRay is my ray.
My code:
while (hitTwo.collider.gameObject != targetHex) {
if (Physics.Raycast(otherRay, out hitTwo, float.MaxValue, layermask)) {
currentHex = hitTwo.collider.gameObject;
otherRay = new Ray(currentHex.transform.position, targetHex.transform.position);
print("found");
}
}
Answer by kylecat6330 · Jan 29 at 02:57 AM
I assume that you have defined hitTwo before this, so that is not the the issue. I think the issues that yes while "while(hitTwo != targetHex)" it would work if it was null. However, I do not think that you can use .collider.gameObject on null.
gameObjects can be null. I have another function that finds targetHex, and when i'm not selecting one it returns as null. Thank you for your answer though!
Your answer