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. Dismiss Notice

Bug Script bug when game object is selected in editor at runtime.

Discussion in 'Editor & General Support' started by joxthebest314, Apr 14, 2021.

Thread Status:
Not open for further replies.
  1. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    I have a script that works just fine in any situations, except when the gameobject he is on is selected (editor view at runtime).

    I really don't understand why this happens, because it also alters its code: I have a variable called TurretBlueprint, that is normally set to null. But, when I select the gameobject, it's not null anymore. It's really weird, because the game works fine unless I click on the gameobject.

    Here are some pictures :

    Sans titre.png

    This is when I don't select the problematic game object (GameManager).

    This is what happens when I launch the game with the GameManager selected:
    Sans titare.png

    Really nothing else has been changed.

    Is this a bug or am I missing something ?

    Thanks for any help.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Put a breakpoint on that Instantiate call that is throwing the error and see why it is null.

    Remember when Unity switches between run and stop in the editor, it basically "tears down" and "builds up" everything, so if you're storing internal references to things, those things might no longer be valid and you need to go reobtain them.
     
  3. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95
    It should be null. The point is, when I select the GameManager, it's not null anymore. The error is normal, because TurretBlueprint is a class with gameobjects as parameters, and because selecting the GameManager seems to create one, but with no parameters set (there is an if before the instantiate that checks if TurretBlueprint is null).
     
  4. joxthebest314

    joxthebest314

    Joined:
    Mar 31, 2020
    Posts:
    95

    Here is the code of the problematic script, just in case.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class BuildManager : MonoBehaviour
    5. {
    6.     //pouvoir le chopper partout
    7.     public static BuildManager instance;
    8.  
    9.     [HideInInspector]
    10.     public TurretBlueprint turretToBuild = null;
    11.     private Node selectedNode;
    12.  
    13.     [HideInInspector]
    14.     public Shop shop;
    15.  
    16.     private GameObject canvasManagerGO;
    17.  
    18.     public GameObject buildEffect;
    19.     public GameObject sellEffect;
    20.  
    21.     public TurretUIManager turretUIManager;
    22.     //propriété qui permet de récupérer des infos -> on peut construire si on a une tourelle à construire
    23.     public bool canBuild { get{ return turretToBuild != null; }}
    24.     //on regarde si on a l'argent pour la tourelle
    25.     public bool hasMoney { get{ return PlayerStats.money >= turretToBuild.cost; }}
    26.  
    27.     public bool hasATurret = false;
    28.     public GameObject turretToBuildHologram;
    29.     public Vector3 offset = new Vector3(0,5,0);
    30.     private TurretBlueprint previousTurretToBuild;
    31.  
    32.  
    33.     void Awake()
    34.     {
    35.         if(instance != null)
    36.         {
    37.             Debug.Log("il y a déjà un BuildManager");
    38.             return;
    39.         }
    40.         instance = this;
    41.  
    42.         shop = GameObject.FindGameObjectWithTag("MainCanvas").transform.Find("Shop").GetComponent<Shop>();
    43.         canvasManagerGO = GameObject.FindGameObjectWithTag("MainCanvas");
    44.         turretUIManager = canvasManagerGO.GetComponent<TurretUIManager>();
    45.         turretToBuild = null;
    46.     }
    47.  
    48.     void Update()
    49.     {
    50.         if(turretToBuild != previousTurretToBuild)
    51.         {
    52.             hasATurret = false;
    53.             Destroy(turretToBuildHologram);
    54.         }
    55.         Debug.Log(turretToBuild);
    56.         if(turretToBuild != null)
    57.         {
    58.             ShowTurretBlueprint();
    59.  
    60.             if(Input.GetMouseButtonDown(1))
    61.             {
    62.                 turretToBuild = null;
    63.                 Destroy(turretToBuildHologram);
    64.                 shop.DeselectAllTurret();
    65.             }
    66.         }
    67.  
    68.         previousTurretToBuild = turretToBuild;
    69.  
    70.     }
    71.  
    72.     public void SelectTurretToBuild(TurretBlueprint turret)
    73.     {
    74.         turretToBuild = turret;
    75.         DeselectNode();
    76.     }
    77.  
    78.     //getter qui permet de chopper le blueprint
    79.     public TurretBlueprint GetTurretToBuild()
    80.     {
    81.         return turretToBuild;
    82.     }
    83.  
    84.     public void SelectNode(Node node)
    85.     {
    86.         if(node == selectedNode)
    87.         {
    88.             //désélectionner la tourelle si on reclique dessus
    89.             DeselectNode();
    90.             return;
    91.         }
    92.  
    93.         selectedNode = node;
    94.         turretToBuild = null;
    95.  
    96.         //faire apparaître le menu vendre et amélioration
    97.         turretUIManager.SetTarget(node);
    98.         shop.DeselectAllTurret();
    99.     }
    100.  
    101.     public void DeselectNode()
    102.     {
    103.         selectedNode = null;
    104.         turretUIManager.Hide();
    105.     }
    106.  
    107.     void ShowTurretBlueprint()
    108.     {  
    109.         if(!hasATurret)
    110.         {
    111.             turretToBuildHologram = (GameObject)Instantiate(turretToBuild.hologram, new Vector3(0, 0, 0), Quaternion.identity);
    112.             turretToBuildHologram.SetActive(false);
    113.             hasATurret = true;
    114.         }
    115.  
    116.         if(EventSystem.current.IsPointerOverGameObject())
    117.         {
    118.             turretToBuildHologram.SetActive(false);
    119.         }
    120.  
    121.     }
    122. }
    123.  
     
  5. brightskyway

    brightskyway

    Joined:
    Nov 19, 2020
    Posts:
    8
    I have the same problem.
    My player script rotates the object. But when the object is selected and opened in the Inspector, the script rotates the objects much faster.
    If to select the object, but lock the Inspector to do not show its scripts etc, then the problem does not appear.

    Very strange and frustrating.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    It's almost as strange and frustrating as incomplete necro-posting requests.

    Please don't necro post for new problems.

    If you have a problem, make a new complete post. It's FREE!

    When you post, remember nobody here can read your mind.

    This means that you must communicate your technical issue with perfect clarity.

    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 documentation you used to cross-check your work (CRITICAL!!!)

    If you have no idea what is going on, fix that first. Here's how:

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

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

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    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 names of the GameObjects or Components involved?
    - 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.

    Visit Google for how to see console output from builds. 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 for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    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

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
Thread Status:
Not open for further replies.