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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Cast FileInfo to Type of C# Scritp

Discussion in 'Scripting' started by MF5, Jun 3, 2015.

  1. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
    Say I have:
    "FileInfo f;"
    How can I cast that to a:
    "System.Type"
    To be able to do:
    "gameObject.GetComponent<(System.Type) f>(); // Doesn't work just a example"
    Any suggestions would be appreciated.Thanks!
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
  3. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    If you got confused after reading the answer in the link I gave you, the general consensus is "Do not do that"

    Unless you are creating a non game runtime tool, some kind of advanced benchmarking software or proof of concept, you shouldn't be using variable type parameters.

    Tell you what, let's go one step higher. Tell me the problem you're trying to solve, and we'll see if there is a better way to solve it.
     
  5. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
    Sure. I am searching a directory for a list of files. If the extension has a .cs then we want to do gameObject.AddComponent<file>()?
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    That sounds like an injection attack waiting to happen; suggest you do something else.
     
  7. MF5

    MF5

    Joined:
    Apr 12, 2015
    Posts:
    15
    That's the idea almost. Players can have full access to change game files...
     
  8. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    I don't like this idea. I don't know what kind of sandbox Unity runs in but if it's not super tight, I could write a cs which deletes their My Documents folder and give it to all my 'friends' ...
     
  9. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    In the editor, it's true that cs filenames correspond with classnames which represent MonoBehaviours, but it's important to understand the difference between a file and a class.

    First of all, for AddComponent to work, it needs to be on an Object that derives from UnityEngine.Component.

    In order for Unity to use your classes, it needs to compile them before the game is run: you cannot compile a class at runtime.

    What you CAN do is create a assetPackage that your game can stream in, and you can use the classes from that, but at no point can you simply look at a file and expect your gameObject to be able to attach it as a component.

    Here's a quick primer on that: http://docs.unity3d.com/Manual/StreamingAssets.html;

    I would suggest reading up a little bit on unity and how it works.