Search Unity

Retype component to custom type

Discussion in 'Scripting' started by Codoet, Sep 13, 2019.

  1. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    7
    Hi,

    I'm trying to get custom script associated with GO(gameobject) with dynamic type. It means that I'm using custom attributes to a sort part of scripts (https://docs.microsoft.com/en-us/do...oncepts/attributes/creating-custom-attributes). First I get all scripts applied to GO then I sort and what now? If I extract all scripts then all of them are type of Component and I need to know exact type of the script applied on the GO for next use and mainly for possibility to compile it. But I cannot figure out how to reach this point.

    I'm thinking about dynamic but they are not compilable in unity and idk how it's supported on another platforms except Windows.

    Possible answer that I have found is: https://stackoverflow.com/questions/1089123/setting-a-property-by-reflection-with-a-string-value

    Any tips/helps?
     
    Last edited: Sep 13, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,171
    If you'd use interfaces instead of attributes, you could just GetComponent<IInterface>()
     
  3. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    7
    Yep, but there is little bit problem with this implementation - I need to access to whole class and it's methods not just defined by interface. Interface will limit my access to not predefined methods.

    But good idea anyway.
     
    Last edited: Sep 13, 2019
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,657
    What is it you're attempting to do exactly?

    You mention attributes, Components attached to GameObjects, reflectively setting properties, and runtime compiling??? What are you trying to compile?

    So instead of all the features of the language/runtime you're attempting to tap into. How about instead what do you plan to use those for?
     
  5. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    7
    Sorry the description of the problem is set up directly. Compilation problem is with dynamic type of variable.
     
    Last edited: Sep 13, 2019
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,657
    Oh. Ok.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,657
    I mean if all you need to do is "retype", by which I assume you mean "cast", a component. Use the "is", "as", and/or (*type*) operations.

    Code (csharp):
    1.  
    2. if(mycomponent is MyCustomComponentType)
    3. {
    4.     var customcast = mycomponent as MyCustomComponentType;
    5.     customcast.MyVariable = 5;
    6. }
    7.  
    But I don't know if that's what you actually need, since you haven't really described what you're attempting to do.

    This sort of approach is usually a sign that you've designed something wrong. If you knew you were trying to set 'MyVariable' on a MyCustomComponentType. You should have just used GetComponent<MyCustomComponentType>.

    Unless that is you may have 5 different component types that all have a 'MyVariable' on it. In which case see @palex-nx 's suggestion of using an interface.
     
  8. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    7
    Lets say that "MyCustomComponentType" can contain any script for example from C# Vector, Vector3d, MyNewSimpleClass, AnotherTotallyDifferrentClass,... so what's now?

    Soz but I mean u are not able to answer my question anyway
     
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,657
    If @palex-nx and I can't answer your question. I assure you, you're going to have a hard time finding someone on this forum who can.

    I assure you it's more likely your question is poorly formed.
     
    palex-nx likes this.
  10. Codoet

    Codoet

    Joined:
    Jun 22, 2019
    Posts:
    7

    Resolved. I had been using old version of .NET (3.5.x) which is deprecated nowdays in settings under player. Now I'm able to compile dynamic type of variable.
     
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,657
    I'm happy you found it for yourself. You had the information necessary for yourself to answer your own question.
     
    Codoet likes this.