Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Marker script working in editor, but not in build

Discussion in 'Scripting' started by kolja_k, Feb 2, 2022.

  1. kolja_k

    kolja_k

    Joined:
    Dec 12, 2021
    Posts:
    1
    Hey everyone,
    so i am kinda new to scripting and working with Unity in VR and i am all out of ideas what could be wrong here, really hope that maybe one of you can help me out! My problem is:
    I have a script which enables a marker to basicly draw on objects tagged as "Taggable" in Unity. In the editor playmode everything works perfectly fine, but when i build an run the project it stops working. What's even more confusing for me : the script is supposed to also freeze the rotation of the marker once it hits a taggable object so it doesnt flop around when u try to draw with it and this doesnt work in the first scene but it does in the second, while drawing doesnt work at all (again all this works fine in the Editor) . Does anybody have any idea what i could be doing wrong? The marker Script is the following:


    Code (CSharp):
    1.  
    2. using System.Linq;
    3. using UnityEngine;
    4.  
    5. public class Marker : MonoBehaviour
    6. {
    7.     [SerializeField] private Transform _tip;
    8.     [SerializeField] private int _penSize = 5;
    9.  
    10.     private Renderer _renderer;
    11.     private Color[] _colors;
    12.     private float _tipHeight;
    13.  
    14.     private RaycastHit _touch;
    15.     private Taggable _taggable;
    16.     private Vector2 _touchPos, _lastTouchPos;
    17.     private bool _touchedLastFrame;
    18.     private Quaternion _lastTouchRot;
    19.     void Start()
    20.     {
    21.         _renderer = _tip.GetComponent<Renderer>();
    22.         _colors = Enumerable.Repeat(_renderer.material.color, _penSize * _penSize).ToArray();
    23.         _tipHeight = _tip.localScale.y;
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         Draw();
    31.     }
    32.  
    33.     private void Draw()
    34.     {
    35.  
    36.         if (Physics.Raycast(_tip.position, transform.up, out _touch, _tipHeight))
    37.         {
    38.             if (_touch.transform.CompareTag("Taggable"))
    39.             {
    40.                 if (_taggable == null)
    41.                 {
    42.                     _taggable = _touch.transform.GetComponent<Taggable>();
    43.                 }
    44.  
    45.                 _touchPos = new Vector2(_touch.textureCoord.x, _touch.textureCoord.y);
    46.  
    47.                 var x = (int) (_touchPos.x * _taggable.textureSize.x - (_penSize / 2));
    48.                 var y = (int) (_touchPos.y * _taggable.textureSize.y - (_penSize / 2));
    49.  
    50.                 if (y < 0 || y > _taggable.textureSize.y || x < 0 || x > _taggable.textureSize.x) return;
    51.  
    52.  
    53.                 if (_touchedLastFrame)
    54.                 {
    55.                     _taggable.texture.SetPixels(x, y, _penSize, _penSize, _colors);
    56.  
    57.                     for (float f = 0.01f; f < 1.00f; f += 0.01f)
    58.                     {
    59.                         var lerpX = (int) Mathf.Lerp(_lastTouchPos.x, x,f);
    60.                         var lerpY = (int) Mathf.Lerp(_lastTouchPos.y, y,f);
    61.                         _taggable.texture.SetPixels(lerpX, lerpY, _penSize, _penSize, _colors);
    62.                     }
    63.  
    64.  
    65.                     transform.rotation = _lastTouchRot;
    66.                     _taggable.texture.Apply();
    67.                 }
    68.  
    69.                 _lastTouchPos = new Vector2(x, y);
    70.                 _lastTouchRot = transform.rotation;
    71.                 _touchedLastFrame = true;
    72.                 return;
    73.             }
    74.         }
    75.  
    76.         _taggable = null;
    77.         _touchedLastFrame = false;
    78.     }
    79. }
    80.  
     
    Last edited: Feb 2, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Not really but I question your raycast, as traditionally one would not raycast up but rather forward. But perhaps you already know this detail in how you have the drawing object oriented.

    Beyond that, 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 put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    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.

    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:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. Webertob

    Webertob

    Joined:
    Dec 12, 2020
    Posts:
    9
    I have a similar problem. In my application I am using Raycasts to paint into a texture. However RaycastHit.textureCoord returns wrong data in the standalone build (0.0, 0.0). But the same code works fine in the editor / game mode.

    What is strange it, the error somehow depends on the meshes used for the part. But it can be reproduced in 2020.3 and 2021.2, also using multiple differed projects and types of scripts. I have filled a bug.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    First thing I would check is turning off texture compression, perhaps enabling read/write on the texture... might be some magic sauce there.