A part of code does not work on android or windows

Hi,
I have a code that enemy can find a cover when player is shooting, but it only works fine on unity editor, When I build project for Windows or Android enemy don’t take cover.
this is my code :

This is inside of my TakeCover method and returns true if enemy could find a cover and returns false if no free and secure cover are available for him.

looks into crystal ball

All I see is my reflection. Hope that helps!

4 Likes

First up, [ code ] tags when posting code. It makes it easier to read, which makes people more likely to spend time helping you.

Secondly, just telling us it doesn’t do what you want isn’t enough for us to help. How is it meant to work? Specifically where is it failing?

Thirdly, what have you tried to solve the problem yourself? We’re not here to make your game for you. We’re happy to help, but you also have to help yourself. Also, if you have tried stuff, it’s likely that the first things we think of are also the first things you thought of, so if you tell us about them we a) know more about the problem and b) won’t waste time telling you to do them again.

Fourth… there’s a great link in my sig down below. Hit it up!

3 Likes

Reflection is probably not the solution for this. :stuck_out_tongue:

If I was in the scripting forum I might help. Unfortunately I’m unable to provide technical support in the general discussion forum. Says so in the forum rules. ;).

5 Likes

Thank you for reply, I’m new here and I didn’t know there is a [ code ] tag.

I saw that link in your sig, so I must say : English is not my native language; please excuse typing errors.

Hmm, I have some gameobjects with “Cover” tag in my game and I attached a script that enemy can understand that Cover is free and secure or not, I have an EnemyAI script that has TakeCover method, I tried to find all Covers and find those are free and secure,In the next step , enemy choose the nearest cover. When I run my game on android device and enemy see player, enemy only starts shooting and follows the player, It seems this part of code doesn’t run at all, I tried to find out what happened with output_log.txt inside my project folder, but i didn’t understand anything. This was written into the output_log.txt:

(Filename: Line: -1)

NullReferenceException: Object reference not set to an instance of an object
at EnemyAI.TakeCover () [0x00000] in :0

at EnemyAI.Update () [0x00000] in :0

Also I checked my Tag list , All tags are correct.

What confused me is that the game runs properly in the Unity editor and I don’t see any error in console.

Please tell me if you need anything like screenshots, codes , … to reply,

Thankyou

This is an ultra common error for newbies, and it’s critical that you learn what it means and how to manage it. Basically, one of the variables in your script isn’t set to a value, and when the game tries to access it things break.

If you build your game with development and debugging ticked the error message should have a bit more detail for you.

2 Likes

Try

if (!enemyCover) {
//etc
}
1 Like

Or I can give you a really bad advice. use try catch finally. It will at least not give the error but at same time not magically assign the correct value to var. If will just stop the code in try block from executing and go to catch statement… Or if its an android issue are you sure you are not using #if thing (forgot what it is called (again :P));

1 Like

Wired you don’t know what NullReferenceExpection is but you are using enemyCover == null

preprocessor directives

If it is happening on windows when run from the editor then try debugging (see link in signature if you aren’t familiar with debugging).

Also, as Yash said, wrap the code in a try…catch block and output the stack trace:

try
{
...your code...
}
catch (Exception ee)
{
  Debug.Log(ee.StackTrace);
}

This should get you a line number and you can go from there.

1 Like

I would modify this slightly to add a “throw” as well to make sure it gets logged and the exception still happens:

try
{
...your code...
}
catch (Exception ee)
{
  Debug.Log(ee.StackTrace);
  throw;
}
2 Likes