Raycast works in editor but not in build

Hi,

Hope everyone is doing great?

I have a weird issue where a raycast (I assume since I have no errors to determine) is working in the editor, but not in the build.

I have a player who needs to be able to push crates around. In order to make the crates feel realistic, when the player is not pushing them, they have a heavy mass and gravity. When the player is pushing them, the mass and gravity decreases.

Both the player and crate have a rigidbody2d and box collider with isTrigger = false.

This is what I have on the player controller Update method:

Physics2D.queriesStartInColliders = false;
        RaycastHit2D hit =  Physics2D.Raycast(_interractableCheck.position, Vector2.right * _facingDirection, _interractableCheckDistance, _whatIsInterractable);

        if (hit.collider) {
            _isPushing = true;
            _speed = _pushSpeed;
        } else {
            _isPushing = false;
            _speed = _moveSpeed;

I have a crate controller script attached to the crate:

using UnityEngine;

public class CrateController : MonoBehaviour
{
    private Rigidbody2D _rigidbody2D;
    private PlayerController _player;
    private float _originalMass;
    private float _originalGravityScale;
    [SerializeField] private float _newMass;
    [SerializeField] private float _newGravityScale;

    private void Awake() {
        _rigidbody2D = GetComponent<Rigidbody2D>();
        _player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
    }

    private void Start() {
        _originalMass = _rigidbody2D.mass;
        _originalGravityScale = _rigidbody2D.gravityScale;
    }

    private void Update() {
        if (_player._isPushing) {
            _rigidbody2D.mass = _newMass;
            _rigidbody2D.gravityScale = _newGravityScale;
        } else {
            _rigidbody2D.mass = _originalMass;
            _rigidbody2D.gravityScale = _originalGravityScale;
        }
    }
}

This works perfectly in the editor, but as soon as I do a build, the player is unable to move the crates.

Is there something I am missing? Is there a better way to do this?

Many Thanks,

J

That seems weird. Perhaps script execution order?

To find out if the code is even running and finding the collider, or if it’s finding it and something else is going wrong, use this approach:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

ALSO, just as an aside, I recommend always using named arguments with Physics.Raycast() because it contains many poorly-designed overloads:

Hi,

Thank you for the response. I don’t understand why it would work in Unity, but when I do an Android build, it doesn’t work. I will try to add some UI Text in the build to check what is going on.

I don’t get any errors in the console when running in Unity, so have nothing to troubleshoot at the moment.

Many Thanks,

J

Super common, google if you doubt me.

There are ten billion different possible reasons, usually resulting in an error that causes other code to malfunction.

Hi,

I definitely believe you, it is just annoying because it is so much harder to track down in the build :slight_smile:

Many Thanks,

J

Hi,

Just as an update, I figured out the issue. I have some child objects as part of my Player and a couple of these had inherited the “Player” tag. This confused the Crate Controller script as I assume it found multiple tags with the same name and wasn’t able to find the Player Controller script. I changed the tags on the child objects of the Player, leaving just the parent object with the “Player” tag and it worked perfectly.

I figured it out by adding in some UI text that would display in the build and could see the Player Controller was not being assigned. That enabled me to focus on possible root causes.

Thanks again,

J

1 Like

I’m having the exact same problem, NPC’s unable to see the player in build. I think I have child objects in the player with a player tag, I’m gonna delete all of them except the parent, see if that works. Thanks in advance :slight_smile:

Probably not. Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

  • Do not TALK about code without posting it.
  • Do NOT post unformatted code.
  • Do NOT retype code. Use copy/paste properly using code tags.
  • Do NOT post screenshots of code.
  • Do NOT post photographs of code.
  • Do NOT attach entire scripts to your post.
  • ONLY post the relevant code, and then refer to it in your discussion.

It worked! Thanks johnc81.
@Kurt_Dekker why would you assume I didn’t have the same problem? It turned out I did and his solution helped me.
Why can’t I just say thanks to the guy that posted an update to tell us how he fixed it?
A ‘thank you’ never hurt anyone and goes a long way.

Because it’s rarely the case that someone has the exact same problem.

I have found that if you are using imported 3D models, checking the “write/read” setting under the imported model setting can fix the raycast in build version.

The way to show appreciation here is to use the Like button.

Your post would likely have triggered a lock of the thread, as has been done many , many , many times , had it been reported.

1 Like