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 from RequireComponent

Discussion in 'Editor & General Support' started by ipopple, Jun 4, 2014.

  1. ipopple

    ipopple

    Joined:
    Jun 1, 2014
    Posts:
    2
    there is a bug about AddComponent and [RequireComponent]

    follow this

    step1 right click gameObject inspector and add a new C# Component named "MyTest" then click to open with MonoDeveloper

    step2 write code like this

    [RequireComponent(typeof(YourComponent))]
    public class MyTest:MonoBehaviour
    {

    }

    and, run in editor.. so, YourComponent will not be added.


    step3 remove MyTest from gameObject and add it again, YouComponent will successfuly be added.


    this bug will save in project. if you don't do step3.. in ver 4.3.4 4.5
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I don't think this is a bug. If you add a RequireComponent, it will be considered from then on, but not for already existing game object, prefabs, ... . This may lead to very annoying results, that prefabs that worked suddenly break because this component was automatically added, but the fields were not properly initialized. Or if you add a wrong type there, all you game objects would suddenly get that component an you would need to delete them all manually.
     
  3. ipopple

    ipopple

    Joined:
    Jun 1, 2014
    Posts:
    2
    yeah ,thanks .

    so, may be not a bug. but if it happend ( may be happend offen) that difficulty of find out what reason..

    because it looks normally..
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I understand what you mean. But I think Unity made the right decision to say, when someone modifies a script, they have to make sure it keeps running as expected.