Ok after 1 day i cant find out what is wrong i followed how Rick is doing the code but mine is giving this error:
I checked the lizard and cactus positions to be the same:
Ok after 1 day i cant find out what is wrong i followed how Rick is doing the code but mine is giving this error:
I checked the lizard and cactus positions to be the same:
Hi Shaktillar,
There is a variable in your IsAttackerInLane method: myLaneSpawner
.
Check where a lane gets assigned to this variable. The y-coordinates of both the defender and the spawner must be equal. Otherwise, the condition gets evaluated to false, and nothing gets assigned to myLaneSpawner
. If the variable is empty, it is null
, thus the NullReferenceException.
Did this help?
See also:
Thats why i sended these pictures.The coordinates are there for Y and for both is equal in this case 1
Ok i fixed is working but i have to change the code .I have to remove the Mathf.Abs then everything works no error and the debug.log shows if cactus should be idle or shoot.
private void SetLaneSpawner()
{
AttackerSpawner spawners = FindObjectsOfType();
foreach (AttackerSpawner spawner in spawners)
{
bool isCloseEnough = (spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon;
// REMOVED ( Mathf.Abs(spawner.transform.position.y - transform.position.y) <= Mathf.Epsilon);
if (isCloseEnough)
{
myLaneSpawner = spawner;
}
}
}
This is strange i dont understand
In your screenshots, I can see that your spawners are parented to another game object. Since the Inspector shows the local position (transform.localPosition
) relative to the parent, one cannot tell whether the global position (transform.position.y
) is 1f as well. That’s only the case if the root object is at y = 0f.
So this picture is during running the game.So did i understand good that this lizard in world pos should be Y = 1 and the Cactus which was instatiated with the MouseOnclick not manualy placed and its Y =1 ?Or im misunderstanding the code?
I know that the paretn sits on diferent coordinate Spawners game object.
Its child Spawner (1) sits on Y =1 coordinate and we are interested about this Y coordinate?
Same goes with Cactus. After i spend 100 credit and place it at the bottom left
Cactus(clone) is the parent and we are interested at this object Y position which is 1.
We dont care about this Parent child which is body.
I don’t think that you are misunderstanding the code but the “Spawner (1)” game object is not the root object. “Spawners” is the root object.
If “Spawners” is at y = 0.001f, the global y-position of “Spawner (1)” would be y = 1.001f.
The same applies to the Cactus.
In your code, you are comparing the transform.position.y
values with one another. That’s the global position. What you could do is to log the y-positions with a Debug.Log into your console to see if they are 1 or something else. Then you know for sure what is going on during runtime.
Our code does not tell us anything about what is happening after we clicked the Play button. The logic of the code might make sense, and the code might be perfect, but if it receives the “wrong” values, we won’t get the result we have been hoping for.
We dont care about this Parent child which is body.
That’s right. Our “logic” sits on the “Cactus (Clone”), and we are interested in the position of the “Cactus (Clone)”, not the “Body”. The child is completely irrelevant for us in this case.
What I wrote above is just a theory. I don’t know if the problem is caused by the global position values. Use Debug.Logs, please, to verify or falsify my assumption. That’s how debugging works.
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.