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

Question Why Does Unity Add Two Instances of a Script?

Discussion in 'Editor & General Support' started by John-B, Apr 5, 2023.

  1. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,252
    This is something that just started happening (2021.3.19f1). When I drag a script to an object in the Inspector, it adds two instances of the script. Every single time. I now have to remember to delete a script every time I add one. Why did this start happening, and how can I stop it?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Do people really drag script assets to add components? There's an Add Component button right there in the inspector.
     
  3. John-B

    John-B

    Joined:
    Nov 14, 2009
    Posts:
    1,252
    I can't speak for other people, but yes, I do. Really. I find dragging and dropping much quicker and easier than navigating the popup, multi-level Add Component menu. (Do people really use that PITA Add Component menu?) And it's worked just fine for me for since Unity 3. Until about a week ago.

    Thanks for the not-at-all helpful reply.
     
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Oh hey, it happens for me, too. Very weird. It only happens when dragging into the inspector; dragging onto the game object in the hierarchy works fine.

    It doesn't happen in 2022.2.9f1, but it does happen in 2021.3.19f1.

    One useful thing, at least: it creates two separate undo states, so you can tap undo once after it double-adds the script to fix it.
     
  5. timsibiski

    timsibiski

    Unity Technologies

    Joined:
    Jun 3, 2021
    Posts:
    38
    This is a known bug and should be fixed on these Unity versions and newer: 2022.2.5f1, 2023.1.0a1, 2023.1.0b1, 2021.3.20f1.

    If any of the minor versions are incorrect in my above list, my apologies, but another way to say this is that it should be fixed in all latest patch versions for the various supported major (year and release) versions.
     
    chemicalcrux likes this.
  6. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Ah, I should've checked the latest point release :p

    Thanks!
     
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    But you can just type in the name of your component in the search bar and hit enter. 1000x quicker.
     
  8. unity_5-ZrjwKnbYLJNA

    unity_5-ZrjwKnbYLJNA

    Joined:
    Nov 17, 2020
    Posts:
    1
    You're really just flat out ignoring the issue. I really think you just shouldn't have replied at all.
     
    cdr9042 likes this.
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Sometimes solving problems involves working around or circumventing said problem.

    And lets be real, dragging scripts assets onto game objects is an absolutely glacial way to add components.
     
  10. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,869
    @spiney199, just take the L. You're in the wrong if you're advocating taking Path B, when both Path A and Path B are meant to work as designed. You can like Path B, but don't tell Path A users they're wrong for using it. It's just a distraction from the point of the thread which a Path A user brought to Unity's attention.
     
    cdr9042 likes this.
  11. timsibiski

    timsibiski

    Unity Technologies

    Joined:
    Jun 3, 2021
    Posts:
    38
    It's true that one method is generally faster than the other, but we offer the functionality to do it in multiple ways because there's a great benefit to having "multiple ways to skin a cat". When you do that, obviously some are going to be less efficient or less used though.

    But think about it this way @spiney199, what if the bug was actually in the add component flow of typing the script in - like the search feature couldn't find it, or it added two from there. That'd be extra annoying and hard to miss before the bug gets on production. So its good that the alternatives exist and really critical that we fix them when the issue happens, because God forbid both methods are borked one after another. That'd be annoying. I think I'd end up adding the attribute that limits components to one - to every script in the game. I'd get tired of the alternative.

    It's fair game to mention that the add component way might be easier, but it really doesn't matter if they want to use the drag and drop method because if that's what they use, we want them to be able to make that choice. Not worth trying to convince otherwise, and there's nothing wrong with them liking that either.

    I appreciate both of you giving your two cents, by the way, but it's not worth debating and definitely not something to get frustrated over. Because the issue is resolved already! :D
     
  12. ddooms

    ddooms

    Joined:
    Jun 10, 2015
    Posts:
    7
    I can confirm this is still an issue for me in 2021.3.19f1.
     
  13. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    The post by the Unity dev above says it's fixed in versions 2021.3.20 and beyond.