Search Unity

Resolved Disabled script causing run time errors.

Discussion in 'Scripting' started by Carcophan, Nov 22, 2022.

  1. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Hello everyone.

    I am trying to work with two versions of the same script.

    An original, which I experiment with. And a copy of that which is renamed, that I’d like to use as a set version of the rules (essentially a ‘test’ and ‘production’ version of the same script).

    ScriptA and ScriptB are on the same player object, and I had hoped to be able to just disable A, and enable B as I go. Visa versa.

    I am not getting an error with just ScriptA. When I add ScriptB, but it is disabled, ScriptA no longer works. It is now failing due to an ‘unassigned object’ error, though the objects are assigned on the ScriptB component.

    If I delete the ScriptB component from the player object itself, then ScriptA works again. So existing, but disabled, causes ScriptA to have an issue that doesn’t otherwise exist.


    I don’t understand why that would be. Can anyone help explain this to me please?


    Thanks all
     
  2. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    657
    All this scriptA and scriptB talk , and i dont see one script posted O,o
     
  3. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    Is the object being referenced by another object that might have trouble deciding which one to use? Disabling a script just means it's no longer getting the Unity treatment of start, update etc.It is very much alive though, you can easily call methods and such.

    It would be much safer to put each version on a separate child object.
     
  4. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Thanks Shack! I hadn't thought it would still run the functions within it. Which makes sense, because the one object would be calling two objects - one of which is disabled because it is attached to the disabled script.

    I hadn't thought of or tried the child object idea, and will give that a shot.
     
  5. Carcophan

    Carcophan

    Joined:
    May 10, 2021
    Posts:
    79
    Due to how I had my player set up, with a networking component, etc, making two children objects, each with its own script, errored out (cannot have more than one network component).

    However, I did this to get around it and it seems to work so far:

    Created two objects instead of the one Player object. So PlayerA and PlayerB, each with ScriptA/ScriptB respectively. Now I can just load the scene with whichever character/script I need. Not sure why I couldn't think of that earlier, but your comment helped get me there, so thanks!